在iOS中缓存视频 [英] Caching videos in ios

查看:341
本文介绍了在iOS中缓存视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法在AVMediaPlayerController上播放视频

I have the following method playing video on AVMediaPlayerController

-(void)sendRequestForVideo
{

        NSString*VideoStr=@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov";
         NSURL  *url = [NSURL URLWithString:VideoStr];
       AVPlayer *player = [AVPlayer playerWithURL:url];

        AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
        controller.player = player;

        [self addChildViewController:controller];
        [self.view addSubview:controller.view];
        controller.view.frame = self.view.frame;
        [player play];


}

我想像在缓存图像一样缓存在这里播放的视频,但是我无法缓存它,并且不了解如何实现此目标,因为与AVfoundation框架有关的事情很多.请提出一些建议,如何将视频存储在nsurl缓存中.谢谢!!

I want to cache the video played here like we cache image but I am not able to cache it and not understanding how should I move to achieve this thing as there are so many things relted to AVfoundation frammework .Kindly give some suggestions that how vides can be stored in nsurl cache .Thanks in advance!

推荐答案

直接的方法是使用NSUrlSession下载视频并使用内置缓存系统保存视频数据.您可以在此SO answer中查看详细信息. .但是这种方式实际上使您下载了两次视频并重新使用它.因此,您最终可能会使用来自用户电话的更多数据.

There straight forward way is to download the video by using NSUrlSession and use build in cache system to save video data. You can check out the details in this SO answer. But this way you are actually downloading the video twice and re-using it. So you might end up using more data from user phone.

使用AVAssetExportSession并将播放的视频保存到文件系统中并在需要时重复使用的另一种方法.

Another way to use AVAssetExportSession and save the played video into file system and reuse it when needed.

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = exportUrl; // consider you have a export url 
[exporter exportAsynchronouslyWithCompletionHandler:^{
    // here your file will be saved into file system 
}];

迅速

let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
let filename = "filename.mp4"
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last!
let outputURL = documentsDirectory.URLByAppendingPathComponent(filename)

exporter?.outputURL = outputURL
exporter?.outputFileType = AVFileTypeMPEG4
exporter?.exportAsynchronouslyWithCompletionHandler({

    print(exporter?.status.rawValue)
    print(exporter?.error)
})

AVAssetExportSession的帮助下,我们实际上是在重新使用播放的视频,因此不会浪费用户电话数据,并且我们也很容易重用.查看下面的文档链接以更好地了解

With the help of AVAssetExportSession we are actually re-using the played video so user phone data will not be wasted and it is easy for us to reuse as well. Check the below documentation links to understand better

这篇关于在iOS中缓存视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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