首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不调用HLS缓存的AVAssetDownloadDelegate方法

不调用HLS缓存的AVAssetDownloadDelegate方法
EN

Stack Overflow用户
提问于 2016-10-17 10:45:00
回答 2查看 3.1K关注 0票数 7

我遵循了给出的用于HLS缓存的这里教程,但该控件从未到达( AVAssetDownloadDelegate的)任何委托。

我有遗漏什么吗?这是我写的代码

代码语言:javascript
复制
- (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.
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-17 13:46:45

我在模拟器上运行代码

模拟器不支持下载HLS流。

当我使用下面提到的委托方法时,我发现了它。

代码语言:javascript
复制
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

}

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

票数 12
EN

Stack Overflow用户

发布于 2021-02-10 20:44:14

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

结果发现,我的应用程序中有另一个对象,它设置了一个背景URL会话。

代码语言:javascript
复制
let configuration = URLSessionConfiguration.background(withIdentifier: "[id]") 

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40084347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档