MPMoviePlayerController breaks/stops after going t

2019-01-31 20:00发布

问题:

I have a MPMoviewPlayerViewController embedded into an UIView object. When I start the player in the embedded mode everything works fine and as expected. If the user then taps onto the fullscreen toggle (or if I change to fullscreen programmatically using setFullscreen:animated) the player goes fullscreen, the movie plays for another second and after that the screen goes black with only a "Loading..." message.

This behavior only appears using iOS 6 (also iPad 6.0 Simulator), on devices running iOS 5 everything works as intended.

The movie source is a local file from the app bundle.

Upon playing & entering fullscreen the debug output is as follows:

2012-09-26 15:24:48.251 [39895:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-09-26 15:24:48.252 [39895:c07] [MPAVController] Autoplay: Disabling autoplay
2012-09-26 15:24:48.262 [39895:c07] [MPAVController] Autoplay: Enabling autoplay
2012-09-26 15:24:48.265 [39895:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-09-26 15:24:48.266 [39895:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-09-26 15:24:48.267 [39895:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-09-26 15:24:48.268 [39895:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-09-26 15:24:48.276 [39895:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2012-09-26 15:24:48.286 [39895:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2012-09-26 15:24:48.938 [39895:c07] [MPAVController] Autoplay: Enabling autoplay
2012-09-26 15:24:48.940 [39895:c07] [MPAVController] Autoplay: Enabling autoplay
2012-09-26 15:24:48.954 [39895:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2012-09-26 15:24:49.006 [39895:c07] [MPAVController] Autoplay: Enabling autoplay
2012-09-26 15:24:49.012 [39895:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

Has somebody an idea why the player stops working?

Edit: Added an example project on github

回答1:

Are you stopping the video in viewWillDisappear: or viewDidDisappear:? Those methods get called when a video enters fullscreen on iOS 6, but not on any earlier iOS versions (a report has been filed at Open Radar for this "bug"). I posted this temporary solution on a similar question:

My temporary solution until the bug is fixed is to check the player's fullscreen Boolean value in viewWillDisappear: and/or viewDidDisappear:. If it returns YES, the movie is entering fullscreen mode and you should refrain from doing anything that might interrupt it.



回答2:

I've solved this problem with a different approach. Since the main reason of the problem is iOS 6 calling viewWillDisappear: and/or viewDidDisappear: methods. I thought that maybe iOS also calling the same methods of MPMoviePlayerViewController. So I've created a Category for MPMoviePlayerViewController and implemented viewWillDisappear: and/or viewDidDisappear: methods. Interestingly it works. (by the way this is not recommended by apple)

Here are the codes;

Header (MPMoviePlayerViewController_FullscreenFix.h)

#import <MediaPlayer/MediaPlayer.h>

@interface MPMoviePlayerViewController (MPMoviePlayerViewController_FullscreenFix)
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
@end

Implementation (MPMoviePlayerViewController_FullscreenFix.m)

#import "MPMoviePlayerViewController_FullscreenFix.h"

@implementation MPMoviePlayerViewController (MPMoviePlayerViewController_FullscreenFix)

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

@end

Now my code is working on both iOS 6.1.3, 5.5.1 and 4.3.5 versions with the exactly same behavior.



回答3:

I solved it by myself. As I add the Movie Player as a subview to a container view I don't need to use the actual view controller created with the MPMoviePlayerViewController which is intended to be used to present it modally or in some other vc hierarchy.

For the single purpose of having a Movie Player view that can be added to some other view as a subview the MPMoviePlayerController's view property is sufficient.

Until iOS 6 both worked but iOS 6 seems to differ in terms of ressource management/lifetime.

The example project is updated with working code.



回答4:

I had the same problem, but with loading a video from a url (on the web)

Previously I:

  1. Subscribed for MPMoviePlayerPlaybackDidFinishNotification notifications
  2. Initialized a MPMoviePlayerViewController (no content url at this stage)
  3. Presented it via presentMoviePlayerViewControllerAnimated:
  4. While it was up on screen, I loaded the streamed url (asynchronously)
  5. When the url came back, I would set the content url on the MPMoviePlayerViewController' moviePlayer

As you said, occasionally the MPMoviePlayerViewController would get stuck and wouldn't dismiss itself when the user taps exit, to fix this, I changed my autoplay order, so the flow became:

  1. Subscribed for MPMoviePlayerPlaybackDidFinishNotification notifications
  2. Initialized a MPMoviePlayerViewController (no content url at this stage)
  3. Set the moviePlayer's shouldAutoplay boolean to NO
  4. Presented it via presentMoviePlayerViewControllerAnimated:
  5. While it was up on screen, I loaded the streamed url (asynchronously)
  6. When the url came back, I would set the content url on the MPMoviePlayerViewController' moviePlayer
  7. Set the moviePlayer's shouldAutoplay boolean to YES

Since those two changes, I have yet to see the controller get stuck



回答5:

I had something similar on iOS 6.

Have you tried forcing the player to play after going fullscreen? By calling [MPMoviePlayerController play] again for instance - this partly solved the problem I had.



回答6:

Check the exact URL after you set the Content URL of the player. It may contains some illegal characters.

    NSLog(@"%@", player.contentURL);

Simulator removes the spaces but the device doesn't. That's what happened to me.



回答7:

The solution is create a property to retain the MPMoviePlayerController class

@property (nonatomic, retain) MPMoviePlayerController *moviePlayerController;

and use the property in your controller

self.moviePlayerController = [[MPMoviePlayerController alloc] init];

[_viewMediaPlayer addSubview:self.moviePlayerController.view];

there is a bug in iOS6 and the MPMoviePlayerController is deallocated when entry in fullscreen mode http://openradar.appspot.com/12327997



回答8:

So for me this solution worked:

if( !( player.playbackState == MPMoviePlaybackStatePlaying ) ) {
    player.shouldAutoplay = YES;
    [player prepareToPlay];
    [player stop];
    [player play];
}

adding both "shouldAutoplay" and "stop"

Regards, Eliza



回答9:

Just add shouldAutoplay boolean to YES after generating the URL It worked for me.

like this:

NSString *path = [[NSBundle mainBundle] pathForResource:videoFileName ofType:@"mp4" inDirectory:nil];
    NSURL *movieURL = [NSURL fileURLWithPath:path];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init]; 

    player.contentURL = movieURL;
    player.controlStyle = MPMovieControlStyleNone;    

    player.shouldAutoplay = YES;
    [player prepareToPlay];
    player.fullscreen = YES;

    [player.view setFrame:[[[[UIApplication sharedApplication] delegate] window] frame]];  // player's frame must match parent's

    [[[[UIApplication sharedApplication] delegate] window] addSubview: player.view];

    [player play];


回答10:

Another way of dealing with this is to use the full screen notification callback:

1) Add a notification for the movie player MPMoviePlayerDidEnterFullscreenNotification. 2) Before you play the movie, set a boolean indicating that the movie is entering fullscreen. 3) Clear the boolean to NO in your full screen callback as well as your movie finished call back. 4) In your viewWillDisappear, check if your boolean to see if your movie is entering full screen, and handle as needed.

Also when presenting an MPMoviePlayerViewController, using the setFullScreen function after presenting can cause the movie to stop on iOS6.



回答11:

My WORKING solution:

I had the same issue, when I try to play video it stops immediately after a second with logs:

[MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
[MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

I solved it putting stop command just before play command:

[playerController stop];
[playerController play];

Now it works perfect!