Find out if MPMoviePlayerController is currently p

2019-03-25 11:45发布

I have one small problem regarding MPMoviePlayerController, i have a link that play movie when i click on that movie button, however when i click on another button the application gets crashed, i need to find how to identify that movie is playing or getting any kind of response

6条回答
再贱就再见
2楼-- · 2019-03-25 12:27

To expand on @Saurabh's answer you can check if the video is playing by

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

where MPMoviePlaybackState is defined as

enum {
   MPMoviePlaybackStateStopped,
   MPMoviePlaybackStatePlaying,
   MPMoviePlaybackStatePaused,
   MPMoviePlaybackStateInterrupted,
   MPMoviePlaybackStateSeekingForward,
   MPMoviePlaybackStateSeekingBackward
};
typedef NSInteger MPMoviePlaybackState;
查看更多
Luminary・发光体
3楼-- · 2019-03-25 12:27

In swift the way to implement @ZKV7's answer is:

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

where the MPMoviePlaybackState enum is:

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

See apple docs for more information about the MPMoviePlayerController.

查看更多
我只想做你的唯一
4楼-- · 2019-03-25 12:28
typedef NS_ENUM(NSInteger, AVPlayerTimeControlStatus) {
    AVPlayerTimeControlStatusPaused,
    AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate,
    AVPlayerTimeControlStatusPlaying
} NS_ENUM_AVAILABLE(10_12, 10_0);


if(self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying)
{
  // is Playing
}
查看更多
该账号已被封号
5楼-- · 2019-03-25 12:35
我只想做你的唯一
6楼-- · 2019-03-25 12:42

There are two parts, usually used in combination;

Register for the MPMoviePlayerPlaybackStateDidChangeNotification e.g. like this:

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

Within the notification handler, you may then check in detail for the actual state - e.g. like this:

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

You can certainly also use the playbackState property without handling the notification that signals changes of it. Still, in most cases that is the right place to do so.

When removing/killing your movie-playback, do not forget to remove the notification handler, e.g. like this:

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:nil];
查看更多
在下西门庆
7楼-- · 2019-03-25 12:46

to check video player is in playing state or not in SWIFT

@IBAction func btnPressPlay(sender: AnyObject) {
        if videoPlayerViewController.moviePlayer.playbackState == MPMoviePlaybackState.Playing
        {
            self.videoPlayerViewController.moviePlayer.stop()
        }
        else {
            self.videoPlayerViewController.moviePlayer.play()
        }
    }
查看更多
登录 后发表回答