Unable to play MP4 video file from mainBundle

2019-01-23 03:17发布

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!

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-23 03:33

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

查看更多
forever°为你锁心
3楼-- · 2019-01-23 03:33

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");
查看更多
放我归山
4楼-- · 2019-01-23 03:34

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

查看更多
The star\"
5楼-- · 2019-01-23 03:37

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

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-23 03:42
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];
查看更多
7楼-- · 2019-01-23 03:47

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.
查看更多
登录 后发表回答