网编基础(4)网络下载音乐和视频

首先在Main.storyboard中添加响应的按钮和进度条,然后连接到控制器中

代码正文:


#import "ViewController.h"

@interface ViewController ()<NSURLSessionDownloadDelegate>
{
    NSURLSessionDownloadTask *task;
    NSURLSession *session;
    NSData *savedData;

}
@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)downloadURLMp3:(UIButton *)sender {

    //创建URL
    NSURL *url = [NSURL URLWithString:@"http://www.itinge.com/music/3/9158.mp3"];

    //创建网络请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //创建网络会话
    session = [NSURLSession sharedSession];

    //创建网络会话加载任务
    task = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        //文件管理创建
        NSFileManager *manager = [NSFileManager defaultManager];
        //拼接本地沙盒路径存储地址
        NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/music.mp3"];
        //将下载的文件保存到本地路径
        [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:NULL];

        if (error) {

            NSLog(@"下载歌曲出现错误");
        }else {

            NSLog(@"歌曲下载成功");
        }


    }];

    //发起网络请求
    [task resume];

}
- (IBAction)downloadURLMp4:(UIButton *)sender {

    //创建URL
    NSURL *url = [NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];

    //创建网络请求
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    //创建网络会话配置对象
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    //创建网络会话
    session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    //创建网络会话任务对象
    task = [session downloadTaskWithRequest:request];

    //发起网络请求
    [task resume];

}
//开始下载就调用(不断的调用)
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    /*
     这个方法在下载的时候不断的调用
     bytesWritten:本次传输下载了多少个字节
     totalBytesWritten:目前已经下载了多少个字节
     totalBytesExpectedToWrite:文件的总大小
     */

    //获取下载的百分比,计算进度条的值
    CGFloat myValue = totalBytesWritten/(CGFloat)totalBytesExpectedToWrite;
    _progressBar.progress = myValue;



}
- (IBAction)pauseAction:(UIButton *)sender {

    //取消的时候,保存数据
    [task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {

        savedData = resumeData;

        task = nil;

    }];

}
- (IBAction)continueAction:(UIButton *)sender {

    //点击继续按钮,将暂停的时候保存的数据恢复下载
    task = [session downloadTaskWithResumeData:savedData];

    //再次发起网络请求
    [task resume];
}

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

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

@end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值