从网络URL预加载/缓存视频

时间:2014-09-07 23:33:07

标签: ios objective-c caching video avasset

我正在使用SCPlayer(AVPlayer子类)播放存储在服务器上的视频

  

SCPlayer.h   https://github.com/rFlex/SCRecorder/blob/master/Library/Sources/SCPlayer.h

     

SCPlayer.m   https://github.com/rFlex/SCRecorder/blob/master/Library/Sources/SCPlayer.m

我希望能够在使用SCPlayer播放之前预先加载视频,我试过这个来存储AVURLssets:

- (AVURLAsset *)addedOrPreloadedVideoAssetWithStringURL:(NSString *)urlString {
    if (![self.preloadedVideoAssets objectForKey:urlString]) {
        [self removePreloadedVideoAssetIfNeededAndAddKey:urlString];
        [self.preloadedVideoAssets setObject:[AVURLAsset URLAssetWithURL:[NSURL URLWithString:urlString] options:nil] forKey:urlString];
    }
    return [self.preloadedVideoAssets objectForKey:urlString];
}
-(void)removePreloadedVideoAssetIfNeededAndAddKey:(NSString *)newKey {
    [self.preloadedVideoAssetsUrlKeys insertObject:newKey atIndex:0];
    if (self.preloadedVideoAssetsUrlKeys.count > PRELOAD_VIDEO_LIMIT) {
        for (int i = PRELOAD_VIDEO_LIMIT ; i < self.preloadedVideoAssetsUrlKeys.count ; i++) {
            [self.preloadedVideoAssets removeObjectForKey:[self.preloadedVideoAssetsUrlKeys objectAtIndex:i]];
            [self.preloadedVideoAssetsUrlKeys removeObjectAtIndex:i]; 
        }
    }
}

用法:

NSString *sampleUrlString = @"http://download.wavetlan.com/SVV/Media/HTTP/H264/Other_Media/H264_test8_voiceclip_mp4_480x320.mp4";
AVURLAsset *asset = [self addedOrPreloadedVideoAssetWithStringURL:sampleUrlString];
[player setItemByAsset:asset];

这成功地允许我使用 [self addedOrPreloadedVideoAssetWithStringURL:sampleUrlString] 播放我已播放的视频,但提前调用它不会预加载视频(参考应用程序:Vine)。

我怎样才能做到这一点? (如果需要,我已准备好AFNetworking)

0 个答案:

没有答案