MPMoviePlayerPlaybackDidFinishNotification is call

2019-01-29 09:55发布

问题:

I'm trying to play video with the following code:

UIGraphicsBeginImageContext(CGSizeMake(1,1));

self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.blablabla.org/app/video/v5_seq8_v2_360p.mp4"]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlayback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player.moviePlayer];

[self presentMoviePlayerViewControllerAnimated:self.player];

UIGraphicsEndImageContext();

[self.player.moviePlayer prepareToPlay];
[self.player.moviePlayer play];

The sel.player closes and didFinishPlayback: method is called instantly. I retrieve the error from the notification like this:

NSLog(@"Error %@", notification.userInfo[@"error"]);

and it looks like the following:

Domain=MediaPlayerErrorDomain Code=-11850 "Operation Stopped"

but when i open the url in a browser then the video starts playing without any problems. What can be the cause of this error? Thanks

回答1:

I'am not sure why, but with remote url's I realized in a past project that you need to add the observer without object as reference.... If you ask me why, I am not sure. Try adding the observer with nil as object...

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];


回答2:

If the video plays on your desktop machine, but not iOS (either in-app or in Safari), it's most likely because the HTTP server does not support byte-range requests. If the HTTP server does not support byte-range requests, iOS will not play the video. This differs from Safari on OS X, which plays the video regardless.

From the Safari Web Content Documentation:

HTTP servers hosting media files for iOS must support byte-range requests, which iOS uses to perform random access in media playback. (Byte-range support is also known as content-range or partial-range support.) Most, but not all, HTTP 1.1 servers already support byte-range requests.

Also, the HTTP server must provide a valid Content-Type header (the URL above also enumerates the valid MIME types). iOS will not play the video if the MIME type is invalid or missing.



回答3:

Try insert this:

[[NSNotificationCenter defaultCenter] removeObserver: self.player
                                                name: MPMoviePlayerPlaybackDidFinishNotification
                                              object: self.player.moviePlayer];

before

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlayback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player.moviePlayer];