开发者

Find out if MPMoviePlayerController is currently playing

I have one small problem regarding MPMoviePlayerController, i have a link that play movie when i cli开发者_运维知识库ck 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


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;


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


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

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


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜