MPMoviePlayerViewController videos queue

Discussion in 'iOS Programming' started by DennisBlah, Jun 15, 2015.

  1. DennisBlah, Jun 15, 2015
    Last edited: Jun 16, 2015

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all, I'm trying to get a MPMoviePlayer to work with multiple videos.
    I can get an observer on my videoPlayer, and find if end of a video has reached by using:
    Code:
    - (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification*)aNotification {
        if(videoPlayer.moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {
            NSLog(@"Playing");
        } else if(videoPlayer.moviePlayer.playbackState == MPMoviePlaybackStatePaused) {
            NSLog(@"Paused");
        } else if(videoPlayer.moviePlayer.playbackState == MPMoviePlaybackStateSeekingBackward) {
            NSLog(@"Backward");
        } else if(videoPlayer.moviePlayer.playbackState == MPMoviePlaybackStateSeekingForward) {
            NSLog(@"Forward");
        } else if(videoPlayer.moviePlayer.playbackState == MPMoviePlaybackStateStopped) {
            NSLog(@"Stopped");
            BOOL playbackEnded = ([[[aNotification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue] == MPMovieFinishReasonPlaybackEnded);
            BOOL endReached = (videoPlayer.moviePlayer.currentPlaybackTime == videoPlayer.moviePlayer.playableDuration);
    
            if (playbackEnded && endReached) {
                if(playVideo < videos.count - 1) {
                    playVideo++;
                    NSURL *video = [self grabFileURL:[videos objectAtIndex: playVideo] fileType:@"mp4"];
                    videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:video];
                    [videoPlayer.moviePlayer prepareToPlay];
                    [videoPlayer.moviePlayer play];
                    [self dismissViewControllerAnimated:NO completion:nil];
                    [self presentMoviePlayerViewControllerAnimated: videoPlayer];
                }
            } else {
                [[NSNotificationCenter defaultCenter] removeObserver:self
      name:MPMoviePlayerPlaybackDidFinishNotification
      object:videoPlayer.moviePlayer];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
    
          } else if(videoPlayer.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted) {
              NSLog(@"Interupted");
          }
    }
    
    But it's not very nice this way, because the movieplayer will dismiss and get back again straight away with the next video. I'm not sure how to play the next video, should I just dismiss the current videoPlayer and load up next one?

    Besides that, it seems I can't get the next and previous track buttons to work.

    Is there any way to setup a 'queue' of videos.

    Anyone can help me in the right direction?

    Currently got AVPlayer to work
    Code:
      AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL: [self grabFileURL:@"vid1" fileType:@"mp4"]];
      AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[self grabFileURL:@"vid2" fileType:@"mp4"]];
      AVPlayerItem *thirdVideoItem = [AVPlayerItem playerItemWithURL:[self grabFileURL:@"vid3" fileType:@"mp4"]];
       
      queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem, thirdVideoItem, nil]];
      AVPlayerLayer *theLayer = [AVPlayerLayer playerLayerWithPlayer: queuePlayer];
      theLayer.frame = self.view.frame;
      [self.view.layer addSublayer: theLayer];
      [queuePlayer play];
    
    However I can only get it in a view, not present it on its own like you can do with the MPMoviePlayer

    Any help?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I have done this and it's a pain in the neck. I don't have the code in front of me but here are a few odds-n-ends.

    You can stop the MoviePlayerViewController from closing itself by unregistering the player from
    MPMoviePlayerPlaybackDidFinishNotification.

    You can start a new video by setting the contentURL to the next/previous video when a currently playing video ends.

    The next and previous track buttons cause changes in the state of the current video and you need to decide what to do based on those changes. The next track button causes the current video to just end. The previous track button first restarts the video. If it's tapped again in some short period of time (0 - 1.5 seconds or so) it then ends the video.

    I wasn't able to figure out a completely clear way of determine from state changes that the previous track button was tapped. What I did in the end was to trawl through the views and find the previous track button. I then added an action to that button so I get a callback and I set a flag indicating that the video is rewinding. While I hate doing that I couldn't find a better way.

    Nothing much has changed in the mediaplayer between iOS 7 and iOS 8.3.

    I didn't bother with the AVQueuePlayer because it doesn't show enough UI.
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Thanks, the unregistering actually did the trick. (Y)
    But too much hassle, so I worked around, removed the user buttons from the MPMoviePlayerViewController, loaded it into my self.view as subview, and drawn my own buttons on top of it, just like the original one, but all working perfect :D

    Also done same like for my audioplayer, scrubber everything works, also the remote controlls

    Stopped with the avqueue player, I find it too much hassle
     

Share This Page