Search code examples
iosmpmovieplayercontroller

How to display ToolBar Buttons on MPMoviePlayerController


I am trying to display UIToolBar with BarButtonItems on MPMoviePlayerController. Not sure how will I implement it.

I am trying to play the video file when user taps on one of the cell of UITableView. At that time I would like to give an option to user to share the video on FB or tweeter.

Not sure how will I display the share BarButtonItem on MPMoviePlayerController. I am trying to implement something similar to the photo app that comes with iPhone.

Can anyone please help me out? Thank you!


Solution

  • MPMovieplayer is not the right choice for this purpose. You can create a custom movie player with AVPlayer(found under AVFoundation.framework) that would serve your purpose. Create any normal ViewController in your project and add an AVPlayer with code something like below:

    -(void)viewDidLoad {
    //prepare player
    self.videoPlayer = [AVPlayer playerWithURL:<# NSURL for the video file #>];
    self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
    
    //prepare player layer
    self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
    self.videoPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.videoPlayerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:self.videoPlayerLayer];
    
    //add player item status notofication handler
    [self addObserver:self forKeyPath:@"videoPlayer.currentItem.status" options:NSKeyValueObservingOptionNew context:NULL];
    
    //notification handler when player item completes playback
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
    }
    
    //called when playback completes
    -(void)playerItemDidReachEnd:(NSNotification *)notification {
    [self.videoPlayer seekToTime:kCMTimeZero]; //rewind at the end of play
    
     //other tasks
     }
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"videoPlayer.currentItem.status"]) {
        //NSLog(@"player status changed");
    
        if (self.videoPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay) {
            //player is ready to play and you can enable your playback buttons here
        }
    }
    }
    

    As this would be normal view controller, you can add a toolbar buttons/buttons to it, for playing/sharing, etc. and trigger the player actions and any other related action like below:

    -(IBAction)play:(id)sender {
        [self.videoPlayer play];
    }
    -(IBAction)pause:(id)sender {
        [self.videoPlayer pause];
    }
    //etc.
    

    Also make sure to remove the observers in your dealloc:

    -(void)dealloc {
        //remove observers
        @try {
        [self removeObserver:self forKeyPath:@"videoPlayer.currentItem.status" context:NULL];
        }
        @catch (NSException *exception) {}
        @try {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
        }
        @catch (NSException *exception) {}
    
        //other deallocations
        [super dealloc];
    }
    

    More detailed and sophisticated explanation of the process can be found under apples companion guide, available here