Search code examples
iosobjective-cmpmovieplayercontrollerstatusbarsubview

MPMoviePlayerViewController Subview


I have a view within my view controller and have it declared as a property _movieSubview. I add a MPMoviePlayerViewController to _movieSubview and the video plays nicely.

-(void)moviePlay{


NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"High Rope" ofType:@"mov"]];
_playerController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
//[self presentMoviePlayerViewControllerAnimated:_playerController];
_playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

[_playerController.moviePlayer prepareToPlay];
_playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
_playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_playerController.moviePlayer play];


/*[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayerDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:_playerController];*/
//---play partial screen---
_playerController.view.frame = CGRectMake(0, 0, 320, 460);
[_movieSubview addSubview:_playerController.view];

}

- (void)moviePlayerDidFinish:(NSNotification *)note
{
    if (note.object == _playerController) {
        NSInteger reason = [[note.userInfo     objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded)
        {
            [_playerController.moviePlayer play];
        }
    }
}

My 2 questions are:

  1. My status bar disappears when I add the MPMoviePlayerViewController inside my subview. How do I keep the status bar?

  2. How can I have the video play twice on repeat, then wait for user to play after that?


Solution

  • You should present _playerController instead of adding as subview. Use this code while creating the instance of MPMoviePlayer:

         [moviePlayer setFullscreen:YES animated:YES];
         moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    
         NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
         [notificationCenter addObserver:self selector:@selector(moviePlayerEvent:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
         [notificationCenter addObserver:self selector:@selector(playBackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
    
        -(void)moviePlayerEvent:(NSNotification*)aNotification{
    
              [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
    
        }
    
       -(void)playBackStateChanged:(id)sender{
    MPMoviePlaybackState playbackState = [moviePlayerViewController.moviePlayer playbackState];
    
    switch (playbackState) {
    
        case MPMoviePlaybackStateStopped :
    
            //play again
            NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
            [notificationCenter removeObserver:self]; 
            break;
    
        case MPMoviePlaybackStatePlaying :
            break;
    
        case MPMoviePlaybackStateInterrupted :
            break;
        }
    }