【问题标题】:Playing a local video using AVPlayer使用 AVPlayer 播放本地视频
【发布时间】:2016-10-29 10:38:15
【问题描述】:

我只是在这上面浪费了大约 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

但我不断收到禁用的视频播放器..

我做错了什么?

【问题讨论】:

  • 是您尝试播放的本地文件,名称为sample.vid,还是您只是将其重命名为您放入此问题的代码?我认为 AVPlayer 可能会使用扩展名(例如movmp4 等)来确定视频的编码。
  • @MichaelDautermann 该文件在此处重命名,但它确实以vid 结尾。我尝试将其重命名为mp4,但这也不起作用。
  • 不要给它扩展名.vid。你确定这是一个.mp4 文件吗?您可以使用 Mac 上的 QuickTime 播放器和/或设备上的移动 Safari 打开该文件吗?
  • 是的,文件没问题(我也尝试了一堆其他文件,同样的问题)。我会尝试完全删除扩展名,但为什么 AVPlayer 会从 NSURL 获取文件的编码呢?
  • @MichaelDautermann 尝试更改扩展名并将其全部删除,但仍然没有。尝试使用在线视频的 URL 是可行的,因此它必须是导致问题的本地文件的 URL。

标签: ios avplayer


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2018-08-21
    • 2018-04-13
    相关资源
    最近更新 更多