用于HLS缓存的AVAssetDownloadDelegate方法未调用 [英] AVAssetDownloadDelegate methods for HLS caching not getting called
本文介绍了用于HLS缓存的AVAssetDownloadDelegate方法未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已按照给出的此处(用于HLS缓存),但控件永远不会到达任何委托(AVAssetDownloadDelegate).
I have followed the tutorial given here for HLS caching, but the control never reaches to any of the delegates ( of AVAssetDownloadDelegate ).
我错过了什么吗? 这是我写的代码
Am I missing anything? Here is code I wrote
- (void)setupAssetDownloader {
NSURL *assetURL = [NSURL URLWithString:@"STREAMING_HOST/video/hls/3729170.m3u8"];
AVURLAsset *hlsAsset = [AVURLAsset assetWithURL:assetURL];
urlSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"assetDowloadConfigIdentifier"];
avAssetDownloadSession = [AVAssetDownloadURLSession sessionWithConfiguration:urlSessionConfiguration assetDownloadDelegate:self delegateQueue:[NSOperationQueue mainQueue]];
// Download movie
avAssetDownloadTask = [avAssetDownloadSession assetDownloadTaskWithURLAsset:hlsAsset assetTitle:@"downloadedMedia" assetArtworkData:nil options:nil];
//@{AVAssetDownloadTaskMinimumRequiredMediaBitrateKey : @(300000)}
[avAssetDownloadTask resume];
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 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.
}
推荐答案
我正在模拟器和
模拟器不支持下载HLS流.
Downloading HLS streams is not supported on simulator.
当我使用下面提到的委托方法时,我就知道了.
I figured it out when I used the delegate method mentioned below.
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
}
And now struggling for the whole day, I found a sample by Apple here and got the real reason behind the problem.
这篇关于用于HLS缓存的AVAssetDownloadDelegate方法未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文