【问题标题】:How to detect MPMoviePlayerController starts a movie?如何检测 MPMoviePlayerController 开始播放电影?
【发布时间】:2013-10-01 11:47:22
【问题描述】:

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

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

【问题讨论】:

标签: ios mpmovieplayercontroller nsnotificationcenter seek nsnotification


【解决方案1】:
    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

【讨论】:

  • 这也检测从暂停状态恢复 - 不仅仅是在电影实际开始时。
【解决方案2】:

快速

添加观察者

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

【讨论】:

  • 我在实际开始播放之前得到 .Playing 状态。无论如何我们可以处理它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2011-04-27
相关资源
最近更新 更多