Search code examples
iosiphonevideoslideshowmpmediapickercontroller

How to determine a mediaplayer has stopped playing in ios


I need to show images and videos as the slide show in an application. I have kept the images and videos link in an array and determining which is video and which is image, and set the slide show timings according to the video time length and 2 sec for each image, Now the problem is when I start the slide show and the video comes and it plays, I cannot determine the video is stopped playing or not?

I am using MPMediaplayer and checking the stopping by

      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
      }

But all the time it is going to the MPMoviePlaybackStatePaused section, when the video is stopped. Can anyone help me why it is going to that condition all the time when video stopped ? or any other method that can help me to determine the video is stopped playing?


Solution

  • Register for the MPMoviePlayerPlaybackStateDidChangeNotification like this

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

    Within the notification handler method, check for the actual state - e.g. like this:

    - (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification
    {
    
      if (player.playbackState == MPMoviePlaybackStateStopped)
      { 
         //stopped playing
    
      } else if (player.playbackState == MPMoviePlaybackStatePlaying) {
    
        //is playing
    
      } else {
    
      }
    }
    

    to remove the observer use this code

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

    refer MPMoviePlayerController_Class playback property doc for more info.