如何检测MPMoviePlayerController启动电影?

时间:2013-10-01 11:47:22

标签: ios mpmovieplayercontroller nsnotificationcenter seek nsnotification

我正在使用MPMoviePlayerController,我如何检测电影何时实际开始播放 - 而不是当用户摆弄搜索控件时?

从我做的测试中,我总是得到一个“加载状态更改”事件,并且每当电影开始时(moviePlayer.loadState == MPMovieLoadStatePlayable)TRUE并且在用户拖动搜索控件之后(即使他拖动它结束到中间 - 不一定是电影的开头)。我如何区分电影开始和寻找?

2 个答案:

答案 0 :(得分:28)

    MPMoviePlaybackState
    Constants describing the current playback state of the movie player.
    enum {
       MPMoviePlaybackStateStopped,
       MPMoviePlaybackStatePlaying,
       MPMoviePlaybackStatePaused,
       MPMoviePlaybackStateInterrupted,
       MPMoviePlaybackStateSeekingForward,
       MPMoviePlaybackStateSeekingBackward
    };
    typedef NSInteger MPMoviePlaybackState;

注册MPMoviePlayerPlaybackStateDidChangeNotification

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

签入此功能MPMoviePlaybackState

- (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification
    {
      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
      }

}

通过

删除通知
 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

参考:MPMoviePlaybackState

答案 1 :(得分:1)

对于swift

添加观察者

let defaultCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter()
defaultCenter.addObserver(self, selector: "moviePlayerPlaybackStateDidChange:", name: MPMoviePlayerPlaybackStateDidChangeNotification, object: nil)

<强>功能

func moviePlayerPlaybackStateDidChange(notification: NSNotification) {
        let moviePlayerController = notification.object as! MPMoviePlayerController

        var playbackState: String = "Unknown"
        switch moviePlayerController.playbackState {
        case .Stopped:
            playbackState = "Stopped"
        case .Playing:
            playbackState = "Playing"
        case .Paused:
            playbackState = "Paused"
        case .Interrupted:
            playbackState = "Interrupted"
        case .SeekingForward:
            playbackState = "Seeking Forward"
        case .SeekingBackward:
            playbackState = "Seeking Backward"
        }

        print("Playback State: %@", playbackState)
    }