iOS: How to use MPMoviePlayerController iOS: How to use MPMoviePlayerController ios ios

iOS: How to use MPMoviePlayerController

Turns out all we have to do is this:

NSURL *movieURL = [NSURL URLWithString:@""];movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];[self presentMoviePlayerViewControllerAnimated:movieController];[movieController.moviePlayer play];
  • movieController is an instance of MPMoviePlayerViewController declared in the .h file.

  • Important: when defining the URL use NSURL's URLWithString method if you want to access the file through a network and use NSURL's fileUrlWithPath if you have the file locally!

  • [movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.

  • If you want to know when the user tapped the done button (the player will be dismissed automatically) you should know that -viewDidAppear is called on the view controller that appears when the player is dismissed. You could set a BOOL variable when the player starts and check for the BOOL in your -viewDidAppear so that you know that -viewDidAppear was called becaouse the player was dismissed. Alternatively you can register for MPMoviePlayerDidExitFullScreen notification, but that didn't work for me.

OR, if this is not working you can just do the following

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"something" ofType:@"mp4"]]];[self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 320)];[self.moviePlayer play];[self.view addSubview:self.moviePlayer.view];
  • self.movieplayer is an instance of MPMoviePlayerController (not MPMoviePlayerViewController). In my experience it's important to declare it as a property (like so: @property (strong, nonatomic) MPMoviePlayerController *moviePlayer;) rather than a simple ivar, because sometimes it just doesn't work if it's an ivar

  • setting the frame is also important, because if we don't set it, the video will not appear at all. The frame can be anything as long as what you define is within the bounds of your view

  • Important: As above, when defining the URL use NSURL's URLWithString method if you want to access the file through a network and use NSURL's fileUrlWithPath if you have the file locally!

You have to try this......

NSString *path = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"mp4"];    player = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];    [player.view setFrame:CGRectMake(0, 0, 320, 320)];    [self presentMoviePlayerViewControllerAnimated:player];    [player.moviePlayer play];     [self.view addSubview:player.view];

Try this:

//Hello guise :- complete process here.//1. add framework in .h MediaPlayer/MediaPlayer.h//2.add delegate in @interface, MPMediaPickerControllerDelegate//3.@property (retain, nonatomic) MPMoviePlayerViewController *moviePlayerController;//4.and synthesize in .m - @synthesize moviePlayerController;//5.IN - tableViewdelegateMethod- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{        //given path for song    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];    NSString *strPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory1,[movieArray objectAtIndex:indexPath.row]];    NSLog(@"strPath %@",strPath);    //path for song.    //convert path in NSURL.    NSURL *videosURL = [NSURL fileURLWithPath:strPath];    moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:videosURL];    [self presentMoviePlayerViewControllerAnimated:moviePlayerController];    [moviePlayerController.moviePlayer play];}//play song on UITableViewCell click.