AVAssetDownloadDelegate方法HLS缓存没有被调用

问题描述:

我已经遵循教程给出here为HLS缓存,但控制永远不会到达任何代表(AVAssetDownloadDelegate)。AVAssetDownloadDelegate方法HLS缓存没有被调用

我错过了什么? 这里是代码我写

- (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. 
} 
+1

无论问题如何,这都是一个非常有用的例子。谢谢! – Warpling

我运行模拟器上的代码和

下载HLS流不支持模拟器。

我想到了当我使用下面提到的委托方法。

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { 

} 

现在挣扎了整整一天,我发现了一个样本由苹果here并得到了问题背后的真正原因。

+1

嘿,我在设备上运行相同的代码,但仍然没有调用我的委托方法,有什么想法? – Sikander