在ios中缓存视频

时间:2017-05-17 10:33:08

标签: ios objective-c video nsurlcache

我有以下方法在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 frammework。请给出一些建议如何将视频存储在nsurl缓存中。提前谢谢!

1 个答案:

答案 0 :(得分:1)

直接的方法是使用editDesignReq = (key, projectTitle, designBrief, projectBudget, timeToEnd) => { timeToEnd = (this.addHours * 60 * 60 * 1000) + (timeToEnd); this.item.update(key, { projectTitle: projectTitle, designBrief: designBrief, projectBudget: projectBudget, timeToEnd: timeToEnd }).then(() => { this.modal.alert() .title('Updated.') .body('Your Design Brief is updated successfully.') .open(); this.router.navigate(['/dashboard/designRequestlist']); }); this.addHours = 0; } 下载视频,并使用内置缓存系统来保存视频数据。您可以查看此SO answer中的详细信息。但是这样你实际上是两次下载视频并重新使用它。因此,您最终可能会使用来自用户手机的更多数据。

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

目标C

AVAssetExportSession

夫特

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) }) 的帮助下,我们实际上正在重新使用播放的视频,因此用户手机数据不会浪费,我们也很容易重复使用。请查看以下文档链接以了解更好