2011-02-25 68 views
0

我想在用戶按下暫停在movieplayer(即從NSNotificationCenter MPMoviePlaybackStatePause),但只有當他們按下暫停時調用一個函數。不幸的是,當用戶向前或向後尋找(MPMoviePlaybackStateSeekingForward或MPMoviePlaybackStateSeekingBackward)時,電影播放器​​會導致自動暫停事件,然後在短暫延遲後繼續搜索事件。是否有任何方法可以檢測到暫停是在沒有來自尋找事件的情況下完成的。區分MPMovieplayer中的暫停事件

謝謝你!

回答

0

通過添加一個計時器解決了這個:

- (void)moviePlayerPlaybackStateDidChanged:(NSNotification *)notification 
    { 
    NSLog(@"VODViewer - moviePlayerPlaybackStateDidChanged()"); 

    if(moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) 
    { 
self.movieIsSeeking = NO; 
    } 

    if(moviePlayerController.playbackState == MPMoviePlaybackStatePaused) 
    { 
if (self.movieWasPaused) 
{ 
    [self.bookmarkTimer invalidate]; 
    self.bookmarkTimer = nil; 

    self.movieWasPaused = NO; 
} 
else 
{ 
    self.bookmarkTimer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(checkToSetBookmark:) userInfo:nil repeats:NO]; 
    self.movieWasPaused = YES; 
} 
    } 

    if(moviePlayerController.playbackState == MPMoviePlaybackStateSeekingForward) 
    { 
    self.movieIsSeeking = YES; 
    } 

    if (moviePlayerController.playbackState == MPMoviePlaybackStateSeekingBackward) 
    { 
    self.movieIsSeeking = YES; 
    } 
} 
1

也許有點晚了你的問題,但我發現了另一個原因來解釋這種現象:

每次嘗試FWD MPMoviePlaybackStateSeekingForward(4)或時間RWD MPMoviePlaybackStateSeekingBackward(5)之前有一個MPMoviePlaybackStatePaused(2)和一個MPMoviePlaybackStatePlaying(1)。因此,您可以在事件前後輕鬆跟蹤[MPMoviePlayerController currentPlaybackTime],並檢查它是否有暫停(兩者值相同),fwd(最初的<)或rwd(初始>最終)。

MPMoviePlayerController *mp1= (MPMoviePlayerController *)[notification object]; 
NSLog(@"Movie State is: %d",[mp1 playbackState]); 
float curPlaybackTime = round(2.0f * [mp1 currentPlaybackTime])/2.0f; 

switch ([mp1 playbackState]) { 
    case 0: 
     NSLog(@"******* video has stopped"); 
     NSLog(@"TIME: %f",[mp1 currentPlaybackTime]); 
     break; 
    case 1: 
     NSLog(@"******* video is playing after being paused or moved."); 
     NSLog(@"TIME: %f",curPlaybackTime); 
     if (curPlaybackTime==st_time) { 
      if(st_time>0.0){ //if 0 it is a starting video. 
       NSLog(@"Sending a Pause event"); 
      }  
     }else if(curPlaybackTime>st_time){ 
      NSLog(@"Sending a Forward event"); 
     }else{ 
      NSLog(@"Sending a Backaward event"); 
     } 

     break; 
    case 2: 
     NSLog(@"******* video is paused"); 
     st_time=round(2.0f * [mp1 currentPlaybackTime])/2.0f; 
     NSLog(@"TIME: %f",st_time); 
     break; 
    case 3: 
     NSLog(@"******* video was interrupted"); 
     break; 
    case 4: 
     NSLog(@"******* video is seeking forward"); 
     st_time=round(2.0f * [mp1 currentPlaybackTime])/2.0f; 
     NSLog(@"TIME: %f",st_time); 
     break; 
    case 5: 
     NSLog(@"******* video is seeking Backwards"); 
     st_time=round(2.0f * [mp1 currentPlaybackTime])/2.0f; 
     NSLog(@"TIME: %f",st_time); 
     break; 
    default: 
     break; 
} 

需要一點點浮輪,因爲我發現暫停的時間有幾毫秒的不同。

+0

是的,它有點遲了,但你的解決方案是好的。謝謝。 – 2011-12-23 21:32:49

+0

這個解決方案的一個問題是,它不區分用戶實際點擊暫停按鈕的時間和用戶點擊「完成」按鈕的時間。點擊完成觸發暫停,然後停止。 – 2013-08-13 17:55:38