Search code examples
iosobjective-cxcodeavplayer

Why is path to movie file in app bundle nil?


I am trying to play a local movie in my app when a button is pressed. However when I press the button I get the following error and the app crashes.

'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

Here is the code I use to play the movie.

NSString *path=[[NSBundle mainBundle]pathForResource:@"koi swimming"   ofType:@"m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.view.frame = self.view.bounds;
[self.view addSubview:controller.view];
[controller.player play];

and I am attaching an image of my bundle file structure.

image of file structure

Could someone help be with getting this to play please?

Expanded images file.

images file expanded


Solution

  • It seems like the file you are trying to find (koi swimming.m4v) is not added to the target Bundle (which is mainBundle here).

    The bundle is the runtime resources of your app and it's different from the build time resources.

    So make sure to add runtime-needed files to the desired targets when you are adding them:

    Add time

    Also, You can add/remove any file from the target from the file inspector like:

    File inspector