How to play video using MPMoviePlayerController?

I use the following code to play videos using MPMoviePlayerController, but the video does not play. Can someone 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]; 
+7
source share
7 answers

This is rather strange, but it seems to be okay if you make your MPMoviePlayerController as a property instead of a local variable. Something seems to be happening backstage. I think this is related to ARC. Do you use ARC?

This is also a problem with adding 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 a simulator, the path is as follows:

/ 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 be simple:

/ 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 instance 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 MPMoviePlayerController as a property of your view controller.

+20
source

Well, there is a big difference between a set of applications and a catalog of documents. I suggest you take a look at this.

First of all, where is the video stored?

If your video is in the document directory, do not add the document directory path to the package 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 application bundle (you added it to your project in Xcode), you should use what is in the jinx answer.

+3
source
 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 } } 
+1
source

Try setting your kit directly and don’t manually configure the file path

 NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"]; 
0
source

The player is initialized with the URL of the video that we want to play (it can be either the path to the local file of the device or the live URL.) After this player is added as a subspecies view of the current view.

Supported video formats using the MPMoviePlayerController class follow

  • .mov.mpv.3gp.mp4

I'm not sure how much this article will help you. I am new to this. I worked on the step-by-step instructions presented in this article.

0
source
 MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 

hope this helps

-one
source

It took me a few minutes to debug the problem, but the answer is pretty simple. Here is the deal:

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

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

The rest remains the same, except that you must set this "movieSourceType" property as follows:

 MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [self presentViewController:moviePlayerView animated:YES completion:^{}]; 
-one
source

All Articles