ios - 未调用 HLS 缓存的 AVAssetDownloadDelegate 方法

标签 ios objective-c swift http-live-streaming offline-caching

我已按照给定的教程进行操作 here用于 HLS 缓存,但控制永远不会到达任何委托(delegate)( AVAssetDownloadDelegate )。

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

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

最佳答案

我在模拟器上运行代码

Downloading HLS streams is not supported on simulator.

我在使用下面提到的委托(delegate)方法时想通了。

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

}

现在折腾了一整天,我找到了苹果的样本here并找出问题背后的真正原因。

关于ios - 未调用 HLS 缓存的 AVAssetDownloadDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40084347/

相关文章:

ios - 取消本地通知后如何停止本地通知声音

ios - 我的 UIStackView 框架彼此堆叠

ios - AFNetworking 2 和后台任务

ios - "' isDeveloperModeEnabled ' is deprecated: This no longer needs to be set during development. "isDeveloperModeEnabled 用于什么

objective-c - "Expected ' ; swift 下的 ' after top level declarator"

ios - 如何快速访问滚动条位置

ios - Swift 如何检测 UITextView 中的链接是否被单击

iOS 企业分布式应用程序终止,没有任何错误日志

ios - swift 的新手,我的程序会在按下打开时崩溃

ios - 何时以及如何使子 UIViewController 无效