NSURLSession实现下载(不能看到下载进度)

NSURLSession实现下载(不能看到下载进度)

/**

 *  下载任务:不能看到下载进度
 */

- (
void )downloadTask
{
   
// 1. 得到 session 对象
   
NSURLSession *session = [ NSURLSession sharedSession ];
   
   
// 2. 创建一个下载 task
   
NSURL *url = [ NSURL URLWithString : @"http://localhost:8080/MJServer/resources/test.mp4" ];
   
NSURLSessionDownloadTask *task = [session downloadTaskWithURL :url completionHandler :^( NSURL *location, NSURLResponse *response, NSError *error) {
       
// location : 临时文件的路径(下载好的文件)
       
       
NSString *caches = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) lastObject ];
       
// response.suggestedFilename 建议使用的文件名,一般跟服务器端的文件名一致
       
NSString *file = [caches stringByAppendingPathComponent :response. suggestedFilename ];
       
       
// 将临时文件剪切或者复制 Caches 文件夹
       
NSFileManager *mgr = [ NSFileManager defaultManager ];
       
       
// AtPath : 剪切前的文件路径
       
// ToPath : 剪切后的文件路径
        [mgr
moveItemAtPath :location. path toPath :file error : nil ];
    }];
   
   
// 3. 开始任务
    [task
resume ];
}
注意:
NSURLSessionDownloadTask下载完成之后,将数据保存在沙盒里面的tmp临时文件中,需要将临时文件将临时文件剪切或者复制Caches文件夹。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVAudioPlayer 本身并不支持下载功能,如果需要监听下载进度,可以使用 NSURLSession 来进行下载,并通过 AVAudioPlayer 播放下载好的音频文件。 具体来说,可以在 NSURLSessionDelegate 协议中实现以下方法: ``` - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{ // 将下载好的文件剪切到指定目录 NSString *destinationPath = [self getDestinationPathForDownloadedFile]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; if ([fileManager fileExistsAtPath:destinationPath]) { [fileManager removeItemAtPath:destinationPath error:nil]; } BOOL success = [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&error]; if (success) { // 下载完成,发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"AVAudioPlayerDownloadProgressNotification" object:nil userInfo:@{@"progress":@(1.0)}]; } } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ float progress = (float)totalBytesWritten / totalBytesExpectedToWrite; // 发送下载进度通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"AVAudioPlayerDownloadProgressNotification" object:nil userInfo:@{@"progress":@(progress)}]; } ``` 在上面的代码中,我们实现NSURLSessionDelegate 协议中的两个方法。在 didFinishDownloadingToURL 方法中,我们将下载好的文件从临时目录剪切到指定目录,并发送下载进度为 1.0 的通知。在 didWriteData 方法中,我们计算出当前的下载进度,并将其作为 userInfo 传递给通知中心。 在 AVAudioPlayer 播放音频文件之前,可以先判断文件是否已经下载完成,如果下载完成,则直接使用 AVAudioPlayer 播放文件,如果未完成,则等待下载完成之后再进行播放。在接收到下载进度通知之后,可以更新 UI 上的下载进度条。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值