MPMoviePlayerController breaks/stops after going to fullscreen in iOS6

帅比萌擦擦* 提交于 2019-11-29 23:00:38
Anthony

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.

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.

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.

user1702073

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

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.

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.

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

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

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];

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!