使用AVPlayer和AVAssetExportSession进行缓存 [英] Caching with AVPlayer and AVAssetExportSession

查看:339
本文介绍了使用AVPlayer和AVAssetExportSession进行缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AVPlayer缓存渐进式下载视频。如何将AVPlayer的项目保存到磁盘?我正在尝试在播放器的currentItem(完全加载)上使用AVAssetExportSession。这段代码给了我 AVAssetExportSessionStatusFailed(操作无法完成)

I would like to cache progressive-download videos using AVPlayer. How can I save an AVPlayer's item to disk? I'm trying to use AVAssetExportSession on the player's currentItem (which is fully loaded). This code is giving me "AVAssetExportSessionStatusFailed (The operation could not be completed)" :

    AVAsset *mediaAsset = self.player.currentItem.asset;

    AVAssetExportSession *es = [[AVAssetExportSession alloc] initWithAsset:mediaAsset presetName:AVAssetExportPresetLowQuality];

    NSString *outPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"out.mp4"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:outPath error:NULL];

    es.outputFileType = @"com.apple.quicktime-movie";
    es.outputURL = [[[NSURL alloc] initFileURLWithPath:outPath] autorelease];
    NSLog(@"exporting to %@",outPath);
    [es exportAsynchronouslyWithCompletionHandler:^{
        NSString *status = @"";

        if( es.status == AVAssetExportSessionStatusUnknown ) status = @"AVAssetExportSessionStatusUnknown";
        else if( es.status == AVAssetExportSessionStatusWaiting ) status = @"AVAssetExportSessionStatusWaiting";
        else if( es.status == AVAssetExportSessionStatusExporting ) status = @"AVAssetExportSessionStatusExporting";
        else if( es.status == AVAssetExportSessionStatusCompleted ) status = @"AVAssetExportSessionStatusCompleted";
        else if( es.status == AVAssetExportSessionStatusFailed ) status = @"AVAssetExportSessionStatusFailed";
        else if( es.status == AVAssetExportSessionStatusCancelled ) status = @"AVAssetExportSessionStatusCancelled";

        NSLog(@"done exporting to %@ status %d = %@ (%@)",outPath,es.status, status,[[es error] localizedDescription]);
    }];

如何成功导出?我正在研究将mediaAsset复制到AVMutableComposition中,但也没有太多运气。

How can I export successfully? I'm looking into copying mediaAsset into an AVMutableComposition, but haven't had much luck with that either.

谢谢!

PS:以下是人们试图完成同样事情的一些问题(但是使用MPMoviePlayerController):

PS: Here are some questions from people trying to accomplish the same thing (but with MPMoviePlayerController):


  • < a href =https://stackoverflow.com/questions/3338237/cache-progressive-downloaded-content-in-mpmovieplayercontroller>在MPMoviePlayerController中缓存渐进式下载内容

同时流式传输并保存视频?

在MPMoviePlayerController成功预加载后将视频缓存到磁盘

推荐答案

<我还没有使用iOS 4.3 SDK,但我很好奇o知道4.3下的mediaAsset.exportable的值,如下所述:

I'm not using the iOS 4.3 SDK yet, but I'd be curious to know the value of mediaAsset.exportable under 4.3, as described here:

http://developer.apple.com/library/ios/#releasenotes/AudioVideo /RN-AVFoundation/_index.html#//apple_ref/doc/uid/TP40010717-CH1-SW4

我尝试过多次修改你的代码,比如从可用的列表中获取预设名称和文件类型,但是我得到了同样的错误。

I've tried a number of modifications to your code, such as getting the preset name and file type from the list of those available, but I'm getting the same error you are.

所以,我决定尝试更低级别的框架,因为文档声明您可以连接AVAssetReader和AVAssetWriter以获得与AVAssetExportSession相同的效果,除非有更多控制。但是,以下代码:

So, I decided to try a lower-level framework, since the documentation states that you can connect an AVAssetReader and an AVAssetWriter to get the same effect as an AVAssetExportSession except with more control. However, the following code:

NSError *readerError = nil;
AVAssetReader *reader = 
  [AVAssetReader assetReaderWithAsset:mediaAsset error:&readerError];
if (readerError) NSLog(@"Error instantiating asset reader: %@",
  [readerError localizedDescription]);

给出以下输出:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '*** -[AVAssetReader initWithAsset:error:] Cannot initialize an instance of 
AVAssetReader with an asset at non-local URL 'http://example.com/test.mp3''

请注意,我使用工作网址对其进行了测试,但已将其替换为上面的假网址。这对我来说就像iOS不支持我们正在寻找的功能。据我所知,AVAssetExportSession在底层使用AVAssetReader,只是在失败时报告的描述性错误要少得多。如果他们只是这样记录它肯定会很好。 AVAssetExportSession的文档没有提及有关需要本地资产的任何信息:

Note that I tested this with a working URL but have replaced it with a fake one above. This looks to me like iOS doesn't support the functionality we're looking for. For all I know AVAssetExportSession uses an AVAssetReader under the hood and just reports a much less descriptive error when it fails. It sure would be nice if they'd just document it as such. The docs for AVAssetExportSession don't mention anything about the asset needing to be local:

http://developer.apple.com/library/ios/#documentation/AVFoundation /Reference/AVAssetExportSession_Class/Reference/Reference.html#//apple_ref/occ/cl/AVAssetExportSession

我知道这不是一个很好的答案,但它将调查推向了一点点。我仍然希望有一些方法可以做到这一点,因为显然我们并不是唯一想要这个功能的人。

I know this isn't much of an answer, but it moves the investigation down the road a little. I'm still really hoping there's some way to do this, because clearly we are not alone in wanting the feature.

这篇关于使用AVPlayer和AVAssetExportSession进行缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆