Unable to play MP4 video file from mainBundle
Asked Answered
M

7

23

So I'm trying to play a simple intro animation video file that I've dragged into my project in XCode and therefore should be able to play from my mainBundle, right?

With this code:

 NSURL *urlString = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"introvideo" ofType:@"mp4"]]; 
MPMoviePlayerController *player  = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player play];

I get this error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

Any help would be great!

Maddock answered 15/11, 2012 at 16:2 Comment(1)
First of all, my problem was that the introvideo.mp4 file wasn't present in the main bundle. Second of all, I needed to make my player object global - this made the video play just fine. Thanks for all the suggestions, guys!Maddock
V
63

This means your code can't find your introvideo.mp4 file. Make sure you have successfully add that file to your bundle. You can check in your project's setting: Copy Bundle Resource. enter image description here

Vaporous answered 15/11, 2012 at 16:6 Comment(2)
Thanks for your answer - where do I find Copy Bundle Resource? Is it a check box? This has never been a problem for me in the past, so it's kinda weird it suddenly popped up now.Maddock
Thanks - adding the video manually to the bundle did fix the crash!Maddock
I
4

Your code isn't correct. Please change accordingly to the following. Still copy your video into your bundle resources.

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"MacBook Pro with Retina Display" ofType:@"m4v"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
                                                 initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release];
playercontroller = nil;
NSLog(@"Video is Playing");
Inflorescence answered 28/3, 2013 at 21:40 Comment(0)
P
4

Xcode 9

Here is an updated image.

Go to Build Phases > Copy Bundle Resources and see if your file is there. If it isn't you can add it by pressing the "+" button.

enter image description here

Proof answered 21/11, 2017 at 4:33 Comment(0)
S
3

It seems that pathForResource:ofType: returns nil. Check that

  1. this file is indeed added to "copy resources" build phase;
  2. you didn't make mistake in the name of file - paths on device are case-sensitive.
Stowers answered 15/11, 2012 at 16:9 Comment(0)
L
3

You need To Check whether That Video Available in your Application's Resource Bundle.

As You mentioned you getting this error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter.

It simply indicate that problem is in the resourcePath,means there is no such file exist there in Resource Bundle.that's Why that pathForResource returns nil path.

You need to put That Video File Again and Make Sure that file Exist in In Resource Bundle.

Then You should Go AHead with Code as Rehan Also posted.

  NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov" inDirectory:@""]];

  MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
  [[player view] setFrame:[self.view bounds]]; // Frame must match parent view
  [self.view addSubview:[player view]];
  [player play];
  [player release];

I hope It may worth full to you.

Thanks

Lansing answered 15/11, 2012 at 16:22 Comment(0)
S
2
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov" inDirectory:@""]];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];
Staves answered 15/11, 2012 at 16:8 Comment(2)
Ok, so the resource hadn't been copied to the bundle and the crash disappeared after I added the file manually. But using your code to play the file does not show the video. It shows the all black MPMoviePlayerController with no controller - any idea why?Maddock
May I know which version of iOS you r using?Staves
K
1

I had put an entire "Sounds" directory into the project, and added it to the "Copy bundle resources" section. It was working fine, but then it started crashing.

The fix was to prepend the directory name to the filenames. Eg, this didn't work:

SKAction.playSoundFileNamed("crash.caf", waitForCompletion: false)

... but this did work:

SKAction.playSoundFileNamed("Sounds/crash.caf", waitForCompletion: false)
Kimmi answered 1/6, 2015 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.