断点续传

//播放音乐 视频

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<NSURLSessionDownloadDelegate>

//session 回话

@property (nonatomic,strong) NSURLSession *currentSession;

//下载任务,下载大文件

@property (nonatomic ,strong)NSURLSessionDownloadTask *downloadTask;

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

//用于保存 暂停下载时 已经获取的数据

@property (nonatomic ,strong) NSData *resumeData;

@property (nonatomic ,strong) AVPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.progressView setProgress:0];

    

    NSLog(@"%@",NSHomeDirectory());

    

    [self createButton];

    

    [self downFile];

}

- (void)createButton {

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];

    btn.backgroundColor = [UIColor redColor];

    [btn setTitle:@"button" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btnPressed) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

//暂停下载任务

- (void)btnPressed {   

    if (self.resumeData) {

        //downloadTaskWithResumeData 这个方法需要一个你已经获得data数据

//        self.resumeData.length;

        _downloadTask = [_currentSession

downloadTaskWithResumeData:self.resumeData];

        [_downloadTask resume];    

        //开始继续下载,就让这个保存数据的变量为空

        self.resumeData = nil;

        //说明要继续下载

    }else {

        //

        

        //调用这个方法,可以获得你取消下载那个时候 已经获得的数据 resumeData

        [self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {

            //保存记录了 下载了多少

            self.resumeData = resumeData;

        }];

    }


}


// 下载一个稍微大一点的 文件


- (void)downFile {

    //1.NSURL

//    http://111.7.130.207/cache/dldir1.qq.com/qqfile/QQforMac/QQ_V4.0.6.dmg

//    NSURL *url = [NSURL URLWithString:@"http://jameswatt.local/123.mp3"];

    NSURL *url = [NSURL URLWithString:@"http://111.7.130.207/cache/dldir1.qq.com/qqfile/QQforMac/QQ_V4.0.6.dmg?ich_args=cdd49b3f25f18c0a344aeeb64422b91e_7071_0_0_6_565b6306a1c9920a65071b6b95e85d86e94f9924aa5c09d856daaa6955b1e10b_dc405cb5256640263cf80aef8d9c85c3_1_0&ich_ip="];


    //2.NSURLRequest

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //3.NSURLSessionConfiguration(回话的配置,用的是默认配置)

    NSURLSessionConfiguration *config =[NSURLSessionConfiguration defaultSessionConfiguration];

    //4.NSURLSession(创建回话)

    _currentSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

    

    //5. downloadTaskWithRequest(用回话创建一个下载任务)

    _downloadTask = [_currentSession downloadTaskWithRequest:request];

    //6.开始任务

    [_downloadTask resume];

}


//下载的代理方法

//只要下载结束,或者下载出错,或者下载停止,都会调用

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

    if (error) {

        NSLog(@"%@",error);

    }else {

        NSLog(@"下载完成");


    }

    

}

//执行下载任务的时候,有数据写入

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {

    //totalBytesWritten 已经写了多少数据

    //totalBytesExpectedToWrite 总共有多少数据

    //下载的进度

    double progess = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;

    NSLog(@"下载进度%f",progess);

    

    [self.progressView setProgress:progess animated:YES];

}

//只有真正下载完成的时候 才会调用

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {

    NSLog(@"下载完成 %@",location);

    

    //寻找沙盒的 路径

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

    //移动的路径

    NSString *desPath = [path stringByAppendingPathComponent:@"xxx.mp3"];

    

//    NSURL *pathUrl  = [NSURL URLWithString:desPath];

    NSURL *pathUrl = [NSURL fileURLWithPath:desPath];


    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    //复制文件到 另一个地方

    [fileManager copyItemAtURL:location toURL:pathUrl error:nil];

    

    NSLog(@"%@",desPath);


    //用于播放音乐

    _player = [AVPlayer playerWithURL:pathUrl];

//    [_player play];

    

}


你可能感兴趣的:(断点续传)