2016-01-20 143 views
1

我想在iOS應用中轉到下一個ViewController時播放電影。如何從頭開始停止MPMoviePlayerViewController

我寫了這條線。

NSURL *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"movie10" ofType:@"mp4"]]; 

    MPMPlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:filePath ]; 
    MPMPlayerController.moviePlayer.backgroundView.backgroundColor = [UIColor clearColor]; 
    MPMPlayerController.moviePlayer.controlStyle = MPMovieControlStyleNone; 
    //MPMPlayerController.view.frame = self.view.frame; 
    MPMPlayerController.view.frame = CGRectMake(0, 0, 320, 600); 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(splashMoviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:MPMPlayerController 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:MPMPlayerController.moviePlayer]; 

    MPMPlayerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill; 
    [MPMPlayerController.moviePlayer setFullscreen:YES animated:NO]; 

    [self.view addSubview:MPMPlayerController.view]; 

    [MPMPlayerController.moviePlayer pause]; 


- (void)splashMoviePlayBackDidFinish:(NSNotification *)notification 
{ 
    UIView *fadeView = [[UIView alloc]initWithFrame:self.view.frame]; 
    fadeView.backgroundColor = [UIColor blackColor]; 
    fadeView.alpha = 0.0f; 
    [self.view addSubview:fadeView]; 

    // Fadeout & remove 
    [UIView animateWithDuration:0.5f 
        animations:^{ 
         fadeView.alpha = 1.0; 
        } 
        completion:^(BOOL finished){ 
         [fadeView removeFromSuperview]; 

         [[NSNotificationCenter defaultCenter] removeObserver:self]; 
         [MPMPlayerController.view removeFromSuperview]; 
        }]; 
} 


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if ((MPMPlayerController.moviePlayer.playbackState==MPMoviePlaybackStateStopped)||(MPMPlayerController.moviePlayer.playbackState==MPMoviePlaybackStatePaused)) 
    { 
     [MPMPlayerController.moviePlayer play]; 
    } 
    else 
    { 
     [MPMPlayerController.moviePlayer pause]; 
    } 
} 

但是,當ViewController出現並從頭播放時,電影不會停止(暫停)。 (當我觸摸視圖時,電影暫停,一旦我觸摸了那裏,電影再次播放)

我應該怎麼做從停止電影開始?

回答

0

有默認屬性 「shouldAutoplay」 可在MPMPlayerController

用戶下面的代碼::

MPMPlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:filePath ]; 
MPMPlayerController.moviePlayer.backgroundView.backgroundColor = [UIColor clearColor]; 
MPMPlayerController.moviePlayer.controlStyle = MPMovieControlStyleNone; 
//MPMPlayerController.view.frame = self.view.frame; 
MPMPlayerController.view.frame = CGRectMake(0, 0, 320, 600); 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(splashMoviePlayBackDidFinish:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] removeObserver:MPMPlayerController 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:MPMPlayerController.moviePlayer]; 

MPMPlayerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill; 
[MPMPlayerController.moviePlayer setFullscreen:YES animated:NO]; 

//*** Change 
[MPMPlayerController.moviePlayer setShouldAutoplay:NO] 
//*** 

[self.view addSubview:MPMPlayerController.view]; 

[MPMPlayerController.moviePlayer pause]; 
+0

非常感謝您! – bao

+0

@baoga我的榮幸 –