VideoDownLoadManager.h
//
// VideoDownLoadManager.h
// DowloadBreakPointDemo
//
// Created by 郭曉敏 on 15/8/8.
// Copyright (c) 2015年 com.jiaoxuebu.gxm. All rights reserved.
//
#import <Foundation/Foundation.h>
@class VideoModel;
@protocol VideoDownLoadManagerDelegate <NSObject>
-(void)videoDidUpdatedProgressWithVideoModel:(VideoModel *)model;
@end
@interface VideoDownLoadManager : NSObject
@property(nonatomic, weak)id<VideoDownLoadManagerDelegate>delegate;
+(instancetype)sharedInstance;
@property(nonatomic, strong)NSOperationQueue *downLoadQueue;
// 用來保存創(chuàng)建的下載管理類屋群,方面以后的對應管理
@property(nonatomic, strong)NSMutableDictionary *httpOperationDict;
@property(nonatomic, strong)NSMutableArray *downVideoArray;
#pragma mark 開始下載
-(void)startAVideoWithVideoModel:(VideoModel *)downLoadVideo;
#pragma mark 暫停下載
-(void)downloadPausewithModel:(VideoModel *)pauseModel;
#pragma mark 斷點繼續(xù)下載
-(void)downloadResumeWithModel:(VideoModel *)resumeModel;
@end
VideoDownLoadManager.m
//
// VideoDownLoadManager.m
// DowloadBreakPointDemo
//
// Created by 郭曉敏 on 15/8/8.
// Copyright (c) 2015年 com.jiaoxuebu.gxm. All rights reserved.
//
#import "VideoDownLoadManager.h"
#import "VideoModel.h"
#import "VideoDownLoadOperation.h"
@interface VideoDownLoadOperation ()
@end
static VideoDownLoadManager *manager;
@implementation VideoDownLoadManager
+(instancetype)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (manager == nil) {
manager = [[VideoDownLoadManager alloc] init];
manager.httpOperationDict = [NSMutableDictionary dictionary];
manager.downVideoArray = [NSMutableArray array];
}
});
return manager;
}
/*經過研究,AFN 的繼續(xù)和暫停下載不是線程安全的,使用會經常出錯*/
#pragma mark 開始下載
-(void)startAVideoWithVideoModel:(VideoModel *)downLoadVideo
{
NSLog(@"........%@", CachesPath);
if (!self.downLoadQueue) {
self.downLoadQueue = [[NSOperationQueue alloc] init];
self.downLoadQueue.maxConcurrentOperationCount = 3;
}
VideoDownLoadOperation *ope = [[VideoDownLoadOperation alloc] initWithDownLoadVideoModel:downLoadVideo];
ope.updateBlock = ^(VideoModel *model){
if (self.delegate && [self.delegate respondsToSelector:@selector(videoDidUpdatedProgressWithVideoModel:)]) {
[self.delegate videoDidUpdatedProgressWithVideoModel:model];
}
};
[self.httpOperationDict setObject:ope forKey:downLoadVideo.flv];
[self.downLoadQueue addOperation:ope];
[self.downVideoArray addObject:downLoadVideo];
[[NSNotificationCenter defaultCenter] postNotificationName:k_newVideoDidStartDown object:nil];
}
#pragma mark 暫停下載
-(void)downloadPausewithModel:(VideoModel *)pauseModel
{
VideoDownLoadOperation *ope = [self.httpOperationDict objectForKey:pauseModel.flv];
if (!ope.isFinished) {
[ope downLoadPause];
}
}
#pragma mark 斷點繼續(xù)下載
-(void)downloadResumeWithModel:(VideoModel *)resumeModel
{
VideoDownLoadOperation *ope = [self.httpOperationDict objectForKey:resumeModel.flv];
if (!ope.isFinished) {
[ope downLoadResume];
}
}
@end
VideoDownLoadOperation.h
//
// VideoDownLoadOperation.h
// DowloadBreakPointDemo
//
// Created by 郭曉敏 on 15/8/9.
// Copyright (c) 2015年 com.jiaoxuebu.gxm. All rights reserved.
//
#import <Foundation/Foundation.h>
@class VideoModel;
typedef void(^VIDEODidUpateBlcok)(VideoModel *);
@interface VideoDownLoadOperation : NSOperation
-(instancetype)initWithDownLoadVideoModel:(VideoModel *)videoModel;
@property(nonatomic, copy)VIDEODidUpateBlcok updateBlock;
// 暫停
-(void)downLoadPause;
// 恢復
-(void)downLoadResume;
@end
VideoDownLoadOperation.m
//
// VideoDownLoadOperation.m
// DowloadBreakPointDemo
//
// Created by 郭曉敏 on 15/8/9.
// Copyright (c) 2015年 com.jiaoxuebu.gxm. All rights reserved.
//
#import "VideoDownLoadOperation.h"
#import "VideoModel.h"
@interface VideoDownLoadOperation ()<NSURLSessionDelegate,NSURLSessionDownloadDelegate>
{
// 用于判斷一個任務是否正在下載
BOOL _isDownLoading;
}
@property(nonatomic, strong)VideoModel *downLoadVideoModel;
// 定義 session
@property(nonatomic, strong)NSURLSession *currentSession;
// 用于可恢復的下載任務的數(shù)據(jù)
@property(nonatomic, strong)NSData *partialData;
// 可恢復的下載任務
@property(nonatomic, strong)NSURLSessionDownloadTask *task;
@end
@implementation VideoDownLoadOperation
-(instancetype)initWithDownLoadVideoModel:(VideoModel *)videoModel
{
self = [super init];
if (self) {
self.downLoadVideoModel = videoModel;
}
return self;
}
-(void)main
{
NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
self.currentSession = [NSURLSession sessionWithConfiguration:configure delegate:self delegateQueue:nil];
self.currentSession.sessionDescription = self.downLoadVideoModel.flv;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.downLoadVideoModel.flv]];
self.task = [self.currentSession downloadTaskWithRequest:request];
[self.task resume];
_isDownLoading = YES;
while (_isDownLoading) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
}
self.partialData = nil;
}
// 暫停
-(void)downLoadPause
{
NSLog(@"暫停");
[self.task suspend];
}
// 恢復
-(void)downLoadResume
{
NSLog(@"恢復下載");
[self.task resume];
}
#pragma mark delegate(task)
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(@"path = %@", location.path);
// 將臨時文件剪切或者復制到 caches 文件夾
NSFileManager *manager = [NSFileManager defaultManager];
NSString *appendPath = [NSString stringWithFormat:@"/%@.mp4",self.downLoadVideoModel.title];
NSString *file = [CachesPath stringByAppendingString:appendPath];
[manager moveItemAtPath:location.path toPath:file error:nil];
_isDownLoading = NO;
// 下載完成發(fā)送通知
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:k_videoDidDownedFinishedSuccess object:self.downLoadVideoModel];
self.downLoadVideoModel.isDownFinished = YES;
});
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"-----%f", bytesWritten * 1.0 / totalBytesExpectedToWrite);
self.downLoadVideoModel.progressValue = totalBytesWritten / (double)totalBytesExpectedToWrite;
dispatch_async(dispatch_get_main_queue(), ^{
if (self.updateBlock) {
self.updateBlock(self.downLoadVideoModel);
}
});
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
NSLog(@"%.0f", fileOffset /(CGFloat) expectedTotalBytes);
}
@end