【问题标题】:AVAssetDownloadDelegate methods for HLS caching not getting called未调用用于 HLS 缓存的 AVAssetDownloadDelegate 方法
【发布时间】:2017-02-26 07:36:18
【问题描述】:

我已经按照教程给出了here 进行 HLS 缓存,但控制权永远不会到达任何代表( 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.
}

【问题讨论】:

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

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


【解决方案1】:

我在模拟器上运行代码,

模拟器不支持下载 HLS 流。

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

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

}

现在苦苦挣扎了一整天,我找到了 Apple here 的样本,并找到了问题背后的真正原因。

【讨论】:

  • 嘿,我在设备上运行相同的代码,但我的委托方法仍然没有被调用,知道吗?
【解决方案2】:

我最近遇到了同样的症状。

原来我的应用中有另一个对象设置了后台 URL 会话。

let configuration = URLSessionConfiguration.background(withIdentifier: "[id]") 

一旦我删除了另一个会话,我就开始收到预期的回调。

也许某处记录了应用程序不应设置多个后台下载会话,但无论如何这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2018-09-12
    • 1970-01-01
    • 2019-08-24
    • 2016-07-30
    • 2018-05-27
    相关资源
    最近更新 更多