Unable to play MP4 video file from mainBundle

狂风中的少年 提交于 2019-11-28 23:05:21

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.

David Moore

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");

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.

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

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.

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 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!