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:47

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