Find out if MPMoviePlayerController is currently p

2019-03-25 12:00发布

问题:

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

回答1:

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;


回答2:

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];


回答3:

You can check playbackState property of MPMoviePlayerController. Refer this link -

http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html



回答4:

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.



回答5:

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


回答6:

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


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