Powered By Blogger

Saturday, October 20, 2012

how to implement MPMoviePlayerViewController in iPhone?

Hi every one,
Please take a look at MPMoviePlayerController integration for local/live video playing in iPhone/iPad/iPod Touch (iOS 4.0 and later..).

Step 1 : Add "MediaPlayer.framework" into your project resource.
Step 2 :  Import <MediaPlayer/MediaPlayer.h> into your class where you want to use MediaPlayer for playing videos (local/live).
Step 3 : Place below code into the controller class where you want to integrate player.

-(IBAction)btnVideoClicked:(id)sender
{
    @try
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        GetVideos *obj_video = [arrVideos objectAtIndex:[sender tag]];
        MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:obj_video.VideoPath]];
        [moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
        [moviePlayerViewController.moviePlayer setShouldAutoplay:YES];
        [moviePlayerViewController.moviePlayer setFullscreen:NO animated:YES];
        [moviePlayerViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [moviePlayerViewController.moviePlayer setScalingMode:MPMovieScalingModeNone];
        [moviePlayerViewController.moviePlayer setUseApplicationAudioSession:NO];
        // Register to receive a notification when the movie has finished playing. 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlaybackStateDidChange:) 
                                                     name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                                   object:moviePlayerViewController];
        // Register to receive a notification when the movie has finished playing. 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlayBackDidFinish:) 
                                                     name:MPMoviePlayerPlaybackDidFinishNotification 
                                                   object:moviePlayerViewController];
        [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
        moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
        [moviePlayerViewController release];
        [pool release];
    }
    @catch (NSException *exception) {
        // throws exception
    }
}


#pragma mark -----------------------
#pragma mark MPMoviePlayer Notification Methods


-(void)moviePlaybackStateDidChange:(NSNotification *)notification
{

    MPMoviePlayerViewController *moviePlayerViewController = [notification object]; 

    if (moviePlayerViewController.moviePlayer.loadState == MPMovieLoadStatePlayable &&
        moviePlayerViewController.moviePlayer.playbackState != MPMoviePlaybackStatePlaying)
    {
        [moviePlayerViewController.moviePlayer play];
    }
   
    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                               object:moviePlayerViewController];
    moviePlayerViewController = nil;
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification

    MPMoviePlayerViewController *moviePlayerViewController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:moviePlayerViewController]; 
    [self dismissMoviePlayerViewControllerAnimated];
    moviePlayerViewController = nil;


Thanks ,
Nilesh M. Prajapati

4 comments:

  1. hi,
    how to more than one videos playing mpmoviewplayercontroller.

    ReplyDelete
  2. you should create an array of nsurl.
    then in moviePlayBackDidFinish function, set contentUrl to the next url in your array and call the play method on the MPMoviePlayerController

    ReplyDelete
  3. Very good tutorial..But i am facing a problem while pressing forward or backward button MPMoviePlayerViewController is dismissing.Any idea about this?

    ReplyDelete
  4. Nice tutorial, I applied this code and run a live video but i problem is video is not running into the deice but running o simulator.

    ReplyDelete