如何检测MPMoviePlayerController开始播放电影?

9
我正在使用MPMoviePlayerController,我如何检测电影实际开始播放的时间——而不是用户操作寻找控制器的时间?
根据我的测试,每当电影开始时,我总是会收到“加载状态更改”事件,并且(moviePlayer.loadState == MPMovieLoadStatePlayable)在用户操作寻找控制器时也是TRUE(即使他从结尾拖动到中间-不一定要到电影的开头)。那我如何区分电影开始和寻找控制器?

也许这个链接有帮助? - AlexR
请查看此链接:https://dev59.com/zm025IYBdhLWcg3wzZT3 - karthika
2个回答

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


这也可以检测到从暂停状态恢复 - 不仅仅是当电影实际开始时。 - Sagi Mann

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

我在实际开始播放之前就得到了“正在播放”的状态。有没有什么方法可以处理它? - MobileGeek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接