2016-10-17 4 views
5

Ich habe das Lernprogramm here für HLS-Caching gefolgt, aber die Steuerung erreicht nie eine der Delegaten (von AVAssetDownloadDelegate).AVAssetDownloadDelegate-Methoden für HLS-Caching nicht aufgerufen werden

Fehle ich etwas? Hier ist Code, den ich schrieb

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

Dies war ein sehr hilfreiches Beispiel, unabhängig von der Frage. Vielen Dank! – Warpling

Antwort

5

Ich lief den Code auf Simulator und

HLS Download-Streams nicht auf Simulator unterstützt.

Ich fand es heraus, als ich die Delegiertenmethode unten erwähnte.

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

} 

Und jetzt für den ganzen Tag zu kämpfen, fand ich eine Probe von Apple here und bekam den wahren Grund für das Problem.

+1

Hey, ich benutze denselben Code auf dem Gerät, aber meine Delegierten Methoden werden nicht aufgerufen, keine Idee? – Sikander

Verwandte Themen