使用 AVPlayer 播放本地视频

Playing a local video using AVPlayer

我在这上面浪费了大约 3 个小时,但我看不出哪里出了问题。

我正在尝试使用 AVPlayer 播放我在本地存储的视频。这就是我启动播放器的方式:

- (void)openVideo:(NSURL *)videoURL {

    NSLog(@"Playing video with the url:\n%@", videoURL);
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];

    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    [self presentViewController:playerViewController animated:YES completion:nil];

}

这是我要经过的NSURL

+ (NSURL *)getURL:(NSString *)itemKey withType:(NSString *)itemType {

    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.%@", itemKey, itemType]]];

    NSURL *itemURL;
    if ([[NSFileManager defaultManager] fileExistsAtPath: dataFilePath]){  // if data exists
        itemURL = [NSURL fileURLWithPath:dataFilePath];
    }
    else {
        NSLog(@"The data requested does not exist! Returning an empty url file...");
        itemURL = nil;
    }
    return itemURL;

}

当我运行openVideo时,我得到的输出是:

Playing video with the url:
/var/mobile/Containers/Data/Application/72C35DC4-9EF1-4924-91F4-EDA4BDB6AAD3/Documents/sample.vid

但我总是遇到禁用的视频播放器..

我做错了什么?

终于解决问题了。我正在下载我的文件并将它们存储为 NSData,并且必须使用 writeToFile 并指定正确的扩展名才能正确读取它们。

NSString *saveFilePath; = [NSTemporaryDirectory()stringByAppendingPathComponent:@"temp.mp4"];

[fileData writeToFile:saveFilePath atomically:YES];
NSURL *filepath = [NSURL fileURLWithPath:saveFilePath];
AVPlayer *player = [AVPlayer playerWithURL:filepath];

希望这对那里的人有所帮助。