How can I play an mp4 resource with AVPlayer?

zakdances picture zakdances · Jul 26, 2012 · Viewed 9.1k times · Source

I have included an mp4 resource in my project navigator. I want to play it in an AVPlayer. I know how to create an AVPlayerItem for the AVPlayer object, and so now I'd like to load the appropriate AVAsset into it pointing to my mp4 resource. The problem is that AVAsset only has an assetWithURL: method to create an AVAsset. I want an assetWithName: method, but that method does not exist. How can I play my mp4 file if I don't have a URL for it? If it's not possible to play via name reference, how can I get a file URL for my mp4 file?

Answer

Marc picture Marc · Nov 29, 2012

To get the URL, use

[[NSBundle mainBundle] URLForResource:@"my_video" withExtension:@"mp4"]