是否可以 "Pause" 和 "Resume" AVAssetDownloadTask?
is it possible to "Pause" and "Resume" AVAssetDownloadTask?
到目前为止,我只找到两个下载视频的选项 "Resume" 或 "cancel"/"Suspend"。有什么可能的方法可以在中间暂停下载视频并从停止的地方恢复下载。我正在使用以下代码下载和存储视频。
// 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
}
这里可以使用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.
如果你想找到你当前的任务状态使用state
属性,它将return当前状态,状态如下
/*
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 past answer
到目前为止,我只找到两个下载视频的选项 "Resume" 或 "cancel"/"Suspend"。有什么可能的方法可以在中间暂停下载视频并从停止的地方恢复下载。我正在使用以下代码下载和存储视频。
// 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
}
这里可以使用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.
如果你想找到你当前的任务状态使用state
属性,它将return当前状态,状态如下
/*
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 past answer