了解MPMoviePlayerController当前是否正在播放

时间:2011-04-26 07:44:10

标签: iphone ios objective-c mpmovieplayercontroller

我有一个关于MPMoviePlayerController的小问题,当我点击那个电影按钮时,我有一个播放电影的链接,但是当我点击另一个按钮时应用程序崩溃,我需要找到如何识别该电影正在播放或得到任何回应

6 个答案:

答案 0 :(得分:21)

要扩展@ Saurabh的答案,您可以检查视频是否正在播放

if(player.playbackState == MPMoviePlaybackStatePlaying)
{
// is Playing
}

其中MPMoviePlaybackState定义为

enum {
   MPMoviePlaybackStateStopped,
   MPMoviePlaybackStatePlaying,
   MPMoviePlaybackStatePaused,
   MPMoviePlaybackStateInterrupted,
   MPMoviePlaybackStateSeekingForward,
   MPMoviePlaybackStateSeekingBackward
};
typedef NSInteger MPMoviePlaybackState;

答案 1 :(得分:5)

有两个部分,通常组合使用;

注册MPMoviePlayerPlaybackStateDidChangeNotification,例如像这样:

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

在通知处理程序中,您可以详细检查实际状态 - 例如像这样:

- (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification
{
  //are we currently playing?
  if (movieController_.playbackState == MPMoviePlaybackStatePlaying)
  { //yes->do something as we are playing...
  }
  else 
  { //nope->do something else since we are not playing
  }
}

您当然也可以使用playbackState属性而不处理发出更改信号的通知。不过,在大多数情况下,这是正确的地方。

删除/删除电影播放时,请不要忘记删除通知处理程序,例如像这样:

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

答案 2 :(得分:2)

答案 3 :(得分:2)

快速实现@ ZKV7的答案是:

if(player.playbackState == MPMoviePlaybackState.Playing)
{
  // is Playing
}

MPMoviePlaybackState枚举为:

enum MPMoviePlaybackState : Int {
   case Stopped
   case Playing
   case Paused
   case Interrupted
   case SeekingForward
   case SeekingBackward
}

有关MPMoviePlayerController的更多信息,请参阅apple docs

答案 4 :(得分:0)

检查视频播放器是否处于SWIFT播放状态

void

答案 5 :(得分:0)

typedef NS_ENUM(NSInteger, AVPlayerTimeControlStatus) {
    AVPlayerTimeControlStatusPaused,
    AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate,
    AVPlayerTimeControlStatusPlaying
} NS_ENUM_AVAILABLE(10_12, 10_0);


if(self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying)
{
  // is Playing
}