AVAssetDownloadDelegate methods for HLS caching not getting called
Asked Answered
R

2

7

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.
}
Reproof answered 17/10, 2016 at 10:45 Comment(1)
This was a very helpful example regardless of the question. Thank you!Bulletproof
R
13

I was running the code on simulator and

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.

Reproof answered 17/10, 2016 at 13:46 Comment(1)
Hey, I'm running the same code on device but still my delegate methods are not called, any idea?Dressmaker
S
1

I recently came across the same symptoms.

Turns out I had another object in my app that setup a background URL session.

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

As soon as I removed that other session, I started to get the expected callbacks.

Maybe it's documented somewhere that an app shouldn't setup multiple background download sessions, but in any case this solved my problem.

Secant answered 10/2, 2021 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.