Find out if MPMoviePlayerController is currently playing
Asked Answered
H

6

10

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

Hardnosed answered 26/4, 2011 at 7:44 Comment(2)
Code and/or crash logs would help.Ambulator
ok let me change the question i have movie running on iPhone simulator, i need to find in another function that wheater movie is playing or notHardnosed
V
21

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;
Vikkivikky answered 26/4, 2011 at 8:27 Comment(1)
In Swift to access the enumeration mentioned above use MPMoviePlaybackState.Playing - see my complete answer below.Soldo
G
5

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];
Grantley answered 26/4, 2011 at 8:36 Comment(0)
V
2

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

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

Vacant answered 26/4, 2011 at 8:10 Comment(0)
S
2

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.

Soldo answered 8/6, 2015 at 1:24 Comment(0)
H
0

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()
        }
    }
Helmholtz answered 10/5, 2016 at 11:41 Comment(0)
W
0
typedef NS_ENUM(NSInteger, AVPlayerTimeControlStatus) {
    AVPlayerTimeControlStatusPaused,
    AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate,
    AVPlayerTimeControlStatusPlaying
} NS_ENUM_AVAILABLE(10_12, 10_0);


if(self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying)
{
  // is Playing
}
Weisler answered 14/11, 2017 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.