区分MPMovieplayer中的暂停事件

时间:2011-02-25 14:50:34

标签: objective-c mpmovieplayercontroller

我试图在用户按下电影播放器​​中的暂停时调用一个函数(即从NSNotificationCenter中按下MPMoviePlaybackStatePaused),但只有当他们按下暂停时才会调用。不幸的是,当用户向前或向后搜索(MPMoviePlaybackStateSeekingForward或MPMoviePlaybackStateSeekingBackward)时,电影播放器​​会导致自动暂停事件,然后在短暂延迟后继续搜索事件。是否有任何方法可以检测到暂停是在没有来自搜索事件的情况下完成的。

谢谢!

2 个答案:

答案 0 :(得分:1)

对于你的问题可能有点迟了但我发现了另一个解释这种行为的原因:

每次尝试FWD MPMoviePlaybackStateSeekingForward(4)或RWD MPMoviePlaybackStateSeekingBackward(5)时,都会有一个MPMoviePlaybackStatePaused(2)和之后的MPMoviePlaybackStatePlaying(1)。因此,您可以在事件之前和之后轻松跟踪[MPMoviePlayerController currentPlaybackTime],并检查它是否暂停(两个值相同),fwd(初始< final)或rwd(初始> final)。

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;
}   

需要一点浮动,因为我发现暂停的时间是几毫秒不同。

答案 1 :(得分: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;
  } 
}