How to detect MPMoviePlayerController starts a movie?

11,242

Solution 1

    MPMoviePlaybackState
    Constants describing the current playback state of the movie player.
    enum {
       MPMoviePlaybackStateStopped,
       MPMoviePlaybackStatePlaying,
       MPMoviePlaybackStatePaused,
       MPMoviePlaybackStateInterrupted,
       MPMoviePlaybackStateSeekingForward,
       MPMoviePlaybackStateSeekingBackward
    };
    typedef NSInteger MPMoviePlaybackState;

Register for the MPMoviePlayerPlaybackStateDidChangeNotification

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

Check in this function 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
      }

}

Remove notification by

 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

Refer :MPMoviePlaybackState

Solution 2

For swift

Add observer

let defaultCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter()
defaultCenter.addObserver(self, selector: "moviePlayerPlaybackStateDidChange:", name: MPMoviePlayerPlaybackStateDidChangeNotification, object: nil)

Function

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)
    }
Share:
11,242
Sagi Mann
Author by

Sagi Mann

Summary: 24 years experience in the hi-tech industry, out of which 21 years experience in dev management, leadership and programming. 4 years experience in IT Management 7 years experience in IT Courses administration and instruction Specialties: Enterprise Cloud & Mobile architecture Project management & leadership Web technologies (JS/NodeJS, HTML5, PHP, ...) Java technologies (JSE, JEE, Hibernate, ...) Python ObjC C/C++ C# I18N

Updated on July 22, 2022

Comments

  • Sagi Mann
    Sagi Mann almost 2 years

    I am using MPMoviePlayerController, how do I detect when the movie actually started playing - as opposed to when the user fiddles with the seek controls?

    From the tests I made, I always get a "load state change" event and (moviePlayer.loadState == MPMovieLoadStatePlayable) is TRUE whenever the movie starts AND after the user dragged the seek control (even if he dragged it from end to middle - not necessarily to the beginning of the movie). How do I distinguish between movie-start and seek?

  • Sagi Mann
    Sagi Mann over 10 years
    this also detects resume from a pause state - not just when the movie actually starts.
  • Arvind
    Arvind about 6 years
    I am getting .Playing state before it is actually starts playing. Is there anyway we can handle it?