iosobjective-cavurlassetavassetdownloadtask

is it possible to "Pause" and "Resume" AVAssetDownloadTask?


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
    }

Solution

  • in here you can use the state of suspend

    A task, while suspended, produces no network traffic and is not subject to timeouts. A download task can continue transferring data at a later time. All other tasks must start over when resumed.

    if you want to find your current task state use state property, it will return the current state , the states are following

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

    for e.g you can use as like

     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
    }
    

    Option 2

    if you want to perform in KVO pattern , please see this SO past answer