如何确定媒体播放器已停止在ios中播放

时间:2013-12-11 12:48:00

标签: ios iphone video slideshow mpmediapickercontroller

我需要在应用程序中以幻灯片形式显示图像和视频。我保持图像和视频链接在一个数组中并确定哪个是视频和哪个是图像,并根据视频时间长度设置幻灯片放映时间,每个图像设置2秒,现在问题是当我开始幻灯片放映时视频来了,它播放,我无法确定视频是否停止播放?

我正在使用MPMediaplayer并通过

检查停止
      if (player.playbackState == MPMoviePlaybackStatePlaying)
      { //playing
      }
      if (player.playbackState == MPMoviePlaybackStateStopped)
      { //stopped
      }if (player.playbackState == MPMoviePlaybackStatePaused)
      { //paused
      }if (player.playbackState == MPMoviePlaybackStateInterrupted)
      { //interrupted
      }if (player.playbackState == MPMoviePlaybackStateSeekingForward)
      { //seeking forward
      }if (player.playbackState == MPMoviePlaybackStateSeekingBackward)
      { //seeking backward
      }

但是当视频停止时,它始终会转到MPMoviePlaybackStatePaused部分。任何人都可以帮助我,为什么视频停止时它会一直处于这种状态?或任何其他方法可以帮助我确定视频停止播放?

2 个答案:

答案 0 :(得分:2)

添加此观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

查看MPMoviePlaybackStatePlaying的通知

- (void) moviePlayerPlaybackStateDidChange: (NSNotification *) notification {
     if (moviePlayer.playbackState == MPMoviePlaybackStateStopped) {
     }
}

答案 1 :(得分:1)

注册MPMoviePlayerPlaybackStateDidChangeNotification这样的

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(MPMoviePlayerPlaybackStateDidChange:) 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                               object:nil];

在通知处理程序方法中,检查实际状态 - 例如像这样:

- (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification
{

  if (player.playbackState == MPMoviePlaybackStateStopped)
  { 
     //stopped playing

  } else if (player.playbackState == MPMoviePlaybackStatePlaying) {

    //is playing

  } else {

  }
}

删除观察者使用此代码

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:nil];

请参阅MPMoviePlayerController_Class playback property doc以获取更多信息。