使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
2015-11-03 14:12
218 查看
断点续传原理:
断点:在点击暂停按钮或者断网的时候记录已经下载的字节数续传:在点击开始按钮的时候或者监听到网络重连的时候将上次记录的字节数通过HTTP请求传给服务器,继续下载
拖两个按钮
连接方法
#pragma mark - 开始 - (IBAction)startAction:(id)sender { } #pragma mark - 暂停 - (IBAction)stopAction:(id)sender { }
声明属性
#import <AVFoundation/AVFoundation.h> @interface ViewController () @property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask; // 下载任务 @property (nonatomic, strong) NSURLSession *session; // 网络 @property (nonatomic, strong) NSData *downloadData; // 下载的数据 @property(nonatomic, strong) AVAudioPlayer * player; // 将player写入属性,用模拟器start项目时必须用属性,否则是没有声音的 player是为了下载完成后播放音乐 @end
遵守下载代理
初始化网络
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; }
开始下载
#pragma mark - 开始 - (IBAction)startAction:(id)sender { if (self.downloadTask) { NSLog(@"继续下载"); NSLog(@"%lu", self.downloadData.length); // 把存储的下载数据给通过网络传递给服务器继续下载 self.downloadTask = [self.session downloadTaskWithResumeData:self.downloadData]; }else{ NSLog(@"开始下载"); self.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:@"http://yinyueshiting.baidu.com/data2/music/244383700/244379959144425526164.mp3?xcode=ee4c4a527b584e3a794b86d808232fc4"]]; } // 开始任务 [self.downloadTask resume]; }
暂停下载
#pragma mark - 暂停 - (IBAction)stopAction:(id)sender { NSLog(@"暂停下载"); [self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) { // 把暂停下载的时候的数据存储到全局变量中 NSLog(@"%lu", resumeData.length); self.downloadData = resumeData; }]; }
下载完成实现音乐播放
#pragma mark - 完成下载实现音乐播放 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"已下载"); // 存储到本地 NSURL * url=[NSURL fileURLWithPath:@"/Users/jiayuanfa/Desktop/music.mp3"]; NSFileManager * manager=[NSFileManager defaultManager]; [manager moveItemAtURL:location toURL:url error:nil]; // 播放音乐 dispatch_async(dispatch_get_main_queue(), ^{ NSData * data=[manager contentsAtPath:@"/Users/jiayuanfa/Desktop/music.mp3"]; self.player = [[AVAudioPlayer alloc] initWithData:data error:nil]; [self.player play]; }); }
监听下载进度
#pragma mark - 监听下载 进度条等等 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ double downloadProgress = totalBytesWritten / (double)totalBytesExpectedToWrite; NSLog(@"%f", downloadProgress); }
运行
点击开始按钮
点击暂停按钮然后再点击开始按钮
下载完成
桌面保存
相关文章推荐
- 微信JS-SDK,获取jsapi_ticket时,一直报错40001错误。
- mysql数据迁移shell 脚本
- 基于chromium for android开发Android浏览器
- HTML 链接
- service controller来安装、卸载、控制服务
- 使用POI读写word docx文件
- 项目代码matlab
- java通过模拟post方式提交表单实现图片上传功能实例
- 2015年10月21日--2015年11月10日(平均每天3小时,15天,共47小时,剩3350小时)
- ibatis的实现
- ibatis的实现
- 从”JAVA“而终 7:java运行过程以及各变量初始化顺序
- myeclipse修改内存大小不足
- Swift 入门基础(结构体/类)
- iOS 开发实践之 Auto Layout
- jar包介绍
- Permutation Sequence
- android linearlayout 的gravity属性
- centos7上搭建jenkins环境
- TestNG同时使用DataProvider和Parameters