ios 无法从 mainBundle 播放 MP4 视频文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13401417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to play MP4 video file from mainBundle
提问by PinkFloydRocks
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?
所以我想播放一个简单的介绍动画视频文件,我已经在 XCode 中拖入我的项目,因此应该能够从我的 mainBundle 播放,对吗?
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'
我收到此错误消息: * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*-[NSURL initFileURLWithPath:]: nil string parameter”
Any help would be great!
任何帮助都会很棒!
回答by sunkehappy
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.
这意味着您的代码无法找到您的 introvideo.mp4 文件。确保您已成功将该文件添加到您的包中。您可以检查项目的设置:复制捆绑资源。
回答by 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");
回答by Suragch
回答by Kreiri
It seems that pathForResource:ofType: returns nil. Check that
似乎 pathForResource:ofType: 返回 nil。检查一下
- this file is indeed added to "copy resources" build phase;
- you didn't make mistake in the name of file - paths on device are case-sensitive.
- 这个文件确实被添加到“复制资源”构建阶段;
- 您没有弄错文件名 - 设备上的路径区分大小写。
回答by Kamar Shad
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
.
正如你提到的
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.
它只是表明问题出在资源路径中,意味着资源包中不存在此类文件。这就是pathForResource
返回nil
路径的原因。
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.
那么你应该继续使用Rehan
同样发布的代码。
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
谢谢
回答by rehan
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];
回答by Ben Wheeler
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'twork:
解决方法是在文件名前添加目录名。例如,这并没有工作:
SKAction.playSoundFileNamed("crash.caf", waitForCompletion: false)
... but this didwork:
...但这确实有效:
SKAction.playSoundFileNamed("Sounds/crash.caf", waitForCompletion: false)