我正在尝试下载HLS视频以供离线播放。没有调用任何委托函数,但
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {}
并显示以下错误:
Error Domain = AVFoundationErrorDomain代码= -11800“该操作可能 未完成” UserInfo = {NSLocalizedFailureReason =未知错误 发生(-12780),NSLocalizedDescription =无法执行该操作 完成}
我的代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupAssetDownloader];
}
- (void)setupAssetDownloader {
NSURL *assetURL = [NSURL fileURLWithPath:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
AVURLAsset *hlsAsset = [AVURLAsset assetWithURL:assetURL];
urlSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"assetDowloadConfigIdentifier"];
//MyDownloadDelegate *delegate = [[MyDownloadDelegate alloc] init];
avAssetDownloadSession = [AVAssetDownloadURLSession sessionWithConfiguration:urlSessionConfiguration assetDownloadDelegate:self delegateQueue:[NSOperationQueue mainQueue]];
// Download movie
avAssetDownloadTask = [avAssetDownloadSession assetDownloadTaskWithURLAsset:hlsAsset assetTitle:@"cbvideo" assetArtworkData:nil options:nil];
//@{AVAssetDownloadTaskMinimumRequiredMediaBitrateKey : @(300000)}
[avAssetDownloadTask resume];
restoredAsset = avAssetDownloadTask.URLAsset;
AVPlayerItem *playerItem = [AVPlayerItem
playerItemWithAsset:restoredAsset];
//AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:avAssetDownloadTask.URLAsset];
AVPlayer *player = [[AVPlayer alloc ] initWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [[AVPlayerLayer alloc ] init];
[playerLayer setPlayer:player];
[playerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:playerLayer];
[player play];
}
#pragma mark - AVAssetDownloadDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
}
- (void)URLSession:(NSURLSession *)session assetDownloadTask:(AVAssetDownloadTask *)assetDownloadTask didResolveMediaSelection:(AVMediaSelection *)resolvedMediaSelection {
}
- (void)URLSession:(NSURLSession *)session assetDownloadTask:(AVAssetDownloadTask *)assetDownloadTask didLoadTimeRange:(CMTimeRange)timeRange totalTimeRangesLoaded:(NSArray<NSValue *> *)loadedTimeRanges timeRangeExpectedToLoad:(CMTimeRange)timeRangeExpectedToLoad {
NSInteger percent = 0;
for (NSValue *value in loadedTimeRanges) {
CMTimeRange timeRange = [value CMTimeRangeValue];
percent += CMTimeGetSeconds(timeRange.duration) / CMTimeGetSeconds(timeRangeExpectedToLoad.duration);
}
percent *= 100;
NSLog(@"Progress: %ld", (long)percent);
}
- (void)URLSession:(NSURLSession *)session assetDownloadTask:(AVAssetDownloadTask *)assetDownloadTask didFinishDownloadingToURL:(NSURL *)location {
NSString *localPath = location.relativePath;
NSLog(@"localPath: %@", localPath);
// TODO: Play downloaded file
// IMPORTANT: Don't move this file to another location.
}