MPMoviePlayerController breaks/stops after going to fullscreen in iOS6

后端 未结 11 1024
南方客
南方客 2020-12-24 07:10

I have a MPMoviewPlayerViewController embedded into an UIView object. When I start the player in the embedded mode everything works fine and as exp

相关标签:
11条回答
  • 2020-12-24 07:18

    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

    0 讨论(0)
  • 2020-12-24 07:21

    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.

    0 讨论(0)
  • 2020-12-24 07:23

    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.

    0 讨论(0)
  • 2020-12-24 07:23

    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];
    
    0 讨论(0)
  • 2020-12-24 07:29

    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.

    0 讨论(0)
  • 2020-12-24 07:32

    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.

    0 讨论(0)
提交回复
热议问题