How to play video using MPMoviePlayerController?
Asked Answered
H

7

7

I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];

    [[moviePlayer view] setFrame:[[self view] bounds]];
    [[self view] addSubview: [moviePlayer view]];


    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

    [moviePlayer play];
Haggard answered 30/5, 2012 at 10:17 Comment(1)
Have you specified the extension of the file?Nash
T
20

It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?

It's also an issue that you've over-appended your path:

// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

When I run your code in the simulator, the path looks like this:

/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

It should just be this:

/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

So just delete this line:

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

And then change your player instantiation to this:

_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

So you should add the MPMoviePlayerController as a property of your containing view controller.

Transference answered 8/6, 2012 at 18:13 Comment(3)
Seems to be you have a given a clear description . I have solved this issue long back. The problem is with setting the frame and added preparetoPlay and fullscreen as YES.Haggard
Hi @Ancientar. You can see in the above comment that I have mentioned that, I forgot to set the frame for MPMoviePlayerController and also added a property prepareToPlay. It workedHaggard
In my case its ARC issue. I have made a property of mediaplayer and it worked for me thanks for the answerJacquie
N
3

All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.

First of all, Where is the video stored?

If your video is in the documents directory, don't append the documents directory path to the bundle path.

Just try with the filePath variable:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];

However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in jinx response.

Nash answered 30/5, 2012 at 10:54 Comment(5)
I tried with FilePath too , the screen becoming black but the video is not playing.Haggard
FYI , I am not going to add the files into my project, I want to get it from the Documents directory onlyHaggard
Any very simple example that how to play a video using the MPMoviePlayerController will be quite enough. I will be really thankful for you if you help me solve this problemHaggard
Yeah the problem probably is that the video is not even there. Do you download it from somewhere? How does it get to the documents directory?Nash
I have copied and pasted there. It is working fine for the Webview case. [webview loadRequest:urlRequest]; Which loads the file in the webview and I can see its playingHaggard
A
1
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
        [moviePlayer.view setFrame:CGRectMake(//set rect frame)];
        moviePlayer.controlStyle =  MPMovieControlStyleDefault; 
        moviePlayer.shouldAutoplay=YES;
        moviePlayer.repeatMode = NO;  
        [moviePlayer setFullscreen:YES animated:NO]; 
        [moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];


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

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code


    }
}
Archangel answered 3/9, 2012 at 9:34 Comment(0)
G
0

Try asking your bundle directly and not setting up the file path manually

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];
Gloria answered 30/5, 2012 at 10:24 Comment(0)
S
0

Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.

Supported video formats by MPMoviePlayerController class are following

  1. .mov .mpv .3gp .mp4

I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article

Selfexcited answered 19/2, 2014 at 7:43 Comment(0)
M
-1
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

hope Help it

Morril answered 30/5, 2012 at 10:38 Comment(0)
B
-1

It took me few mins to debug the problem but the answer is quite simple. Here is the deal:

  • If you want to MPMoviePlayerViewController to play from a web URL use this: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];

  • And if you want MPMoviePlayerViewController to play it from App Bundle use this: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];

The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:

MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];
Berrios answered 3/6, 2013 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.