2

so far i found only two options for downloading video either "Resume" or "cancel"/"Suspend". is there any possible way to pause downloading video in middle and resume the download from where it stopped. I am using below code to download and store the video.

  // Create new background session configuration.
    NSURLSessionConfiguration *urlSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"assetDowloadConfigIdentifier"];
    AVAssetDownloadURLSession *avAssetDownloadSession = [AVAssetDownloadURLSession sessionWithConfiguration:urlSessionConfiguration assetDownloadDelegate:self delegateQueue:[NSOperationQueue mainQueue]];

    NSURL *assetURL = [NSURL URLWithString:@"https://a4i6y2k6.stackpathcdn.com/vistvorigin/smil:4b0d690b7b3bc8ac5da2049f50c80794c762423e.smil/playlist.m3u8"];

    AVURLAsset *hlsAsset = [AVURLAsset assetWithURL:assetURL];

   if (@available(iOS 10.0, *)) {

        AVAssetDownloadTask *avAssetDownloadTask = [avAssetDownloadSession assetDownloadTaskWithURLAsset:hlsAsset assetTitle:@"downloadedMedia" assetArtworkData:nil options:nil];

        if([command isEqualToString:@"resume"]){
            // Start task and begin download
            [avAssetDownloadTask resume];
        }else{
            [avAssetDownloadTask cancel];
        }

    } else {
        // Fallback on earlier versions
    }
4

1 回答 1

1

在这里你可以使用状态suspend

任务在挂起时不会产生网络流量,也不会超时。下载任务可以在以后继续传输数据。所有其他任务必须在恢复时重新开始。

如果要查找当前任务状态使用state 属性,它将返回当前状态,状态如下

 /* 
NSURLSessionTaskStateRunning = 0,                     
NSURLSessionTaskStateSuspended = 1,
NSURLSessionTaskStateCanceling = 2,                   
NSURLSessionTaskStateCompleted = 3,  

例如,您可以像这样使用

 NSURL *assetURL = [NSURL URLWithString:@"https://a4i6y2k6.stackpathcdn.com/vistvorigin/smil:4b0d690b7b3bc8ac5da2049f50c80794c762423e.smil/playlist.m3u8"];

AVURLAsset *hlsAsset = [AVURLAsset assetWithURL:assetURL];

if (@available(iOS 10.0, *)) {

    AVAssetDownloadTask *avAssetDownloadTask = [avAssetDownloadSession assetDownloadTaskWithURLAsset:hlsAsset assetTitle:@"downloadedMedia" assetArtworkData:nil options:nil];
    if(avAssetDownloadTask.state ==  1){
        // Start task and begin download
        [avAssetDownloadTask resume];
    }else{
        [avAssetDownloadTask cancel];
    }


} else {
    // Fallback on earlier versions
}

选项 2

如果您想以 KVO 模式执行,请查看此SO 过去的答案

于 2019-03-14T06:36:05.630 回答