Search code examples
iosobjective-cmpmovieplayercontroller

MPMoviePlayer disappear when clicking on done button


I used MPMoviePlayerController to play video. Here is the code.

// Getting URL from path
    NSURL *url = [NSURL fileURLWithPath:self.moviePlayingTempPath];



// Initialize the movie player view controller with a video URL string
self.playerVC = [[MPMoviePlayerController alloc] initWithContentURL:url];

// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:self.playerVC
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:self.playerVC];

// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:self.playerVC];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moveiPlayBackStateChanged:)
                                             name:MPMoviePlayerPlaybackStateDidChangeNotification
                                           object:self.playerVC];    


[self.playerVC setControlStyle:MPMovieControlStyleEmbedded];

self.playerVC.view.frame = CGRectMake(0, 0, 1024, 648);

[self.view addSubview:self.playerVC.view];

// Start playback
[self.playerVC prepareToPlay];
[self.playerVC play];

It works fine in the embedded mode. I have very unusual behavior in full screen mode. When I go full screen and can play the video without any problem. Then I can click the done button and come to the embedded mode without problem.

Problem is occurring when go full screen and single click on forward or backward seeking buttons. Then player stop playing and show activity indicator with loading text. Then If i click the done button then the player disappears forever. I cant figure out exact problem to this. Actually i don't need forward and backward seeking buttons. Can we block seeking or how can i solve this problem.


Solution

  • I solve this by removing and adding the movie player to the view when user switch from full screen.

    Here is the full code

    -(void)continuePlayingFile{
    NSURL *url = [NSURL fileURLWithPath:self.moviePlayingTempPath];
    
    // Initialize the movie player view controller with a video URL string
    self.playerVC = [[MPMoviePlayerController alloc] initWithContentURL:url];
    
    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver:self.playerVC
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.playerVC];
    
    // Register this class as an observer instead
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.playerVC];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moveiPlayBackStateChanged:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:self.playerVC];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterFullScreen:)
                                                 name:MPMoviePlayerWillEnterFullscreenNotification
                                               object:self.playerVC];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willExitFullScreen:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:self.playerVC];
    
    [self.playerVC setControlStyle:MPMovieControlStyleEmbedded];
    self.playerVC.movieSourceType = MPMovieSourceTypeFile;
    
    self.playerVC.view.frame = CGRectMake(0, 0, 1024, 648);
    
    [self.view addSubview:self.playerVC.view];
    
    // Start playback
    [self.playerVC prepareToPlay];
    [self.playerVC play];
     }
    
    
    
    
    
       -(void)willExitFullScreen:(NSNotification *)note{
            self.isMovieGoFullScreen = NO;
            if(self.isUserClickNextButton){
                //This is to handle user click on next/previous button and then click done button.
                self.isUserClickNextButton=NO;
                [self continuePlayingFile];
            }
        }
    
        -(void)moveiPlayBackStateChanged:(NSNotification*)aNotification{
            if(self.playerVC){
                self.currentPlayerState = self.playerVC.playbackState;
               if(self.currentPlayerState == MPMoviePlaybackStateStopped){
                    self.isUserClickNextButton =YES;
                }
            }
        }