1

我必須在第二個X自動停止播放器,我不知道我必須使用哪種通知。 之後,當用戶點擊屏幕上的任何位置時,播放器將繼續運行視頻。MPMoviePlayerController自動暫停在第二個X

- (IBAction)playMovie:(id)sender { 
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"m4v"]; 
    NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
    _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer]; 

    //here I don't know which notification I have to used 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackPause:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_moviePlayer]; 

    _moviePlayer.controlStyle = MPMovieControlStyleDefault; 
    _moviePlayer.initialPlaybackTime = 2.5; 

    _moviePlayer.shouldAutoplay = YES; 
    [self.view addSubview:_moviePlayer.view]; 
    [_moviePlayer setFullscreen:YES animated:NO]; 
    [_moviePlayer play]; 
} 

我試圖抓住電影幀並分析它,看看是否是時候暫停視頻。

- (void) moviePlayBackPause:(NSNotification*)notification{ 
    // check if it's time to pause the video 
    if([_moviePlayer currentPlaybackTime] == 6.0){ 
     [_moviePlayer pause]; 
    } 
} 
  1. 哪種類型的通知我必須使用捕捉視頻的當前時間?

在此先感謝您的答覆! 親切的問候!

回答

4

您可以使用NSTimer來停止視頻。 應該存在一個更好的方式來做到這一點,但與計時器,你也可以做到這一點。

[_moviePlayer play]; 
[NSTimer scheduledTimerWithTimeInterval:6 //Time in seconds 
           target:self 
           selector:@selector(moviePlayBackPause) //Method called when the timer is completed. 
           userInfo:nil 
           repeats:NO]; 
    } 
} 

- (void) moviePlayBackPause { 
    [_moviePlayer pause]; 
} 
+0

謝謝... @ Haroldo Gondim –

+0

我認爲,當一個用戶要暫停然後手動的情況下,我們還設置了標誌的方法moviePlayBackPause檢查:可以播放暫停。我對嗎? –

+0

是的,如果您只想暫停,請調用[_moviePlayer pause];手動。 –

相關問題