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?

有帮助吗?

解决方案

To get the URL, use

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

其他提示

It will probably be in your main bundle, you can get its URL with

- (NSString*) saveFilePath: (NSString *) add
{
    NSString *filename = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:
                    [[NSBundle mainBundle] pathForResource:add ofType:@"mp4"]isDirectory:NO]]];
    return filename;
}

Copy this into your code and then do [self saveFilePath:@"Name of your file"] to get the URL.

Full code:

AVPlayer *player = [[AVPlayer alloc] initWithURL:[self saveFilePath:@"fileName"];
[player play];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top