iOS: How to use MPMoviePlayerController

70,015

Solution 1

Turns out all we have to do is this:

NSURL *movieURL = [NSURL URLWithString:@"http://example.com/somefile.mp4"];
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!

Solution 2

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];

Solution 3

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.

Solution 4

Try add

[player prepareToPlay];

after

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];

You can read more about MPMoviePlayerController in Apple documentation: http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/doc/c_ref/MPMoviePlayerController

Share:
70,015
Zoltán Matók
Author by

Zoltán Matók

Updated on July 30, 2020

Comments

  • Zoltán Matók
    Zoltán Matók almost 4 years

    I've created a blank project (iOS) and put this in my viewDidLoad:

    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
    MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
    [self presentMoviePlayerViewControllerAnimated:playerController];
    [playerController.moviePlayer play];
    

    When the app starts all I get is a white screen with error messages in the log:

     <Error>: CGContextSaveGState: invalid context 0x0
     <Error>: CGContextClipToRect: invalid context 0x0
     <Error>: CGContextTranslateCTM: invalid context 0x0
     <Error>: CGContextDrawShading: invalid context 0x0
     <Error>: CGContextRestoreGState: invalid context 0x0
    Warning: Attempt to present <MPMoviePlayerViewController: 0x821e3b0> on <ViewController: 0x863aa40> whose view is not in the window hierarchy!
    

    ...and a bunch of lines regarding disabling autoplay. I especially don't understand the line about the view not being part of the hierarchy since it's a blank "Single View Application" iOS project and the code is in ViewController.m. It IS in the view hierarchy.

    I know for a fact that the movie file itself is not the problem because I got it from Apple's sample code on MPMoviePlayer. And although I (seemingly) tried everything written in the sample, I just couldn't get the player to work.

    Here is another try, this time with MPMoviePlayerController (not MPMoviePlayerViewController):

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [player setContentURL:url];
    [player setMovieSourceType:MPMovieSourceTypeFile];
    
    [[player view] setFrame:self.view.bounds];
    [player view].backgroundColor = [UIColor greenColor];
    
    player.scalingMode = MPMovieScalingModeNone;
    player.controlStyle = MPMovieControlModeDefault;
    player.backgroundView.backgroundColor = [UIColor whiteColor];
    player.repeatMode = MPMovieRepeatModeNone;
    
    [self.view addSubview: [player view]];
    [player play];
    

    Similar result, with white screen and errors. Please help....

  • Admin
    Admin over 11 years
    This should be a comment, not an answer. From the docs you just linked but obviously haven't read: "If a movie player is not already prepared to play when you call the play method, that method automatically calls this method". So this can't be the problem. The problem is that the current CoreGraphics context is NULL at the time OP tries to initiate the playback.
  • Zoltán Matók
    Zoltán Matók over 11 years
    Even with the sample in the docs you posted (in the Overview part at the top there are a few lines of code that I guess would be the minimum code to make it work) it doesn't work.
  • vnchopra
    vnchopra almost 11 years
    I have tried out this code and I only see the video when i add it to subview like this: [self.view addSubview:_movieController.view]; But for some reason, the top scrubber is a few pixels further below than usual from the status bar. Is there any way to rectify this? And i get these errors as shown in this question. stackoverflow.com/questions/12822420/… What am I doing wrong?
  • KennyVB
    KennyVB about 10 years
    erhm what's movieArray ?
  • Bhumika
    Bhumika about 6 years
    fileURLWithPath worked for me. It didn't work with urlWithString. Thanks