基于AVPlayer播放器实现(MJPlayer)二.缓存

Round 2----->缓存功能实现

在interface中添加下载按钮

@property (nonatomic,strong) UIButton* downLoadBtn;                      //下载按钮

按照之前的步骤,在initMJPlayerFrame中对添加的控件进行布局,并添加手势来对控制界面进行显示隐藏操作

 /*界面 ROUND 2********************************************************************************************/
    self.downLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.downLoadBtn setImage:[UIImage imageNamed:@"MJPlayer_download"] forState:UIControlStateNormal];
    [self.downLoadBtn setImage:[UIImage imageNamed:@"MJPlayer_not_download"] forState:UIControlStateSelected];
    [self.downLoadBtn addTarget:self action:@selector(downLoadBtnPress:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.downLoadBtn];
    [self.downLoadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-20);
        make.height.equalTo(@(49));
        make.width.equalTo(@(40));
    }];
    
    //添加手势
    singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self addGestureRecognizer:singleTapGestureRecognizer];
    //延迟隐藏控制界面
    [self showAndHideControl:0];
//显示隐藏控制界面
-(void)showAndHideControl:(NSInteger)alphaNum
{
    __weak typeof (self)self_ = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.2f animations:^{
            self_.bottomControlView.alpha = alphaNum;
            self_.downLoadBtn.alpha = alphaNum;
        }];
    });
}
-(void)singleTap:(UITapGestureRecognizer*)tapGesture
{
    __weak typeof (self)self_ = self;
    [UIView animateWithDuration:0.2f animations:^{
        self_.bottomControlView.alpha = 1;
        self_.downLoadBtn.alpha = 1;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.2f animations:^{
                self_.bottomControlView.alpha = 0;
                self_.downLoadBtn.alpha = 0;
            }];
        });
    }];
}

在项目中新建一个下载类 MJDownLoad继承于NSObject,使用单例创建对象,声明下载方法及本地方法,待会在.m文件中实现

MJDownLoad.h
//
//  MJDownLoad.h
//  MJAVPlayer
//
//  Created by 马家俊 on 16/11/4.
//  Copyright © 2016年 MJJ. All rights reserved.
//

#import 
#import 

#define UserDefaults [NSUserDefaults standardUserDefaults]
#define GetFileName  [[strUrl stringByReplacingOccurrencesOfString:@"/" withString:@""]  stringByReplacingOccurrencesOfString:@"." withString:@""]
@interface MJDownLoad : NSObject
/*!单例模式获取MJDownLoad对象
 *\\returns  returns:        返回ParkApply对象
 */
+ (MJDownLoad *)shareInstanceManager;

/*!获取url进行下载
 */
-(void)downLoadWithUrl:(NSString*)strUrl;

/*!获取本地url
 *\\returns  returns:        返回NSString 本地URL或者空字符串
 */
-(NSString*)getLocalVedio:(NSString*)strUrl;
@end

MJDownload单例

static MJDownLoad *g_MJDownLoad;
+ (MJDownLoad *)shareInstanceManager
{
    @synchronized(self)
    {
        if (!g_MJDownLoad)
        {
            g_MJDownLoad = [[MJDownLoad alloc] init];
        }
    }
    return g_MJDownLoad;
}

downLoad方法,使用的是 downloadTaskWithRequest,获取URL后先去cache中查看是否有
VedioCache这个文件夹,如果没有则直接代码创建该文件夹,之后进行下载

-(void)downLoadWithUrl:(NSString*)strUrl
{
    //获取cache路径,该文件夹中的文件不会被同步,清楚缓存直接删除该文件夹下所有子文件即可
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) ;
    NSString *documentsDire = [paths objectAtIndex:0];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //以获取到的视频文件名称作为存储文件名
    NSString *fileName = GetFileName;
    NSString *fileNameWithMP4 = [fileName stringByAppendingString:@".mp4"];
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    //查看是否有该路径,如果没有则创建
    NSString* createPath = [NSString stringWithFormat:@"%@/VedioCache/%@", documentsDire,fileName];
    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath])
    {
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
    }else
    {
        NSLog(@"FileDir is exists.");
    }

    //视频缓存最终路径
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@",createPath,fileNameWithMP4];
    NSURL *url = [NSURL URLWithString:strUrl];
    NSURLRequest *requst = [NSURLRequest requestWithURL:url];

    //开启下载任务
    NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:requst progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        return [NSURL fileURLWithPath:fullPath];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        [UserDefaults setObject:fullPath forKey:fileName];
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"下载完成!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
    [task resume];
}

下载完成后获取下载文件,存在该文件则返回路径,否则返回空字符串

-(NSString*)getLocalVedio:(NSString *)strUrl
{
    NSString* localUrl = [UserDefaults objectForKey:GetFileName];
    if (localUrl) {
        return localUrl;
    }else
    {
        return @"";
    }
}

完成MJDownLoad之后要对MJPlayerView进行调整,让player可以在本地存在缓存时不再请求网络数据
在initMJPlayer这个方法中,对playerItem进行初始化的时候去判定是否存在本地缓存,存在则用本地缓存去初始化,否则正常流程

if (![[[MJDownLoad shareInstanceManager]getLocalVedio:vedioUrl] isEqualToString:@""]) {
        NSURL *sourceMovieUrl = [NSURL fileURLWithPath:[[MJDownLoad shareInstanceManager]getLocalVedio:vedioUrl]];
        AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieUrl options:nil];
        self.playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
        self.downLoadBtn.selected = YES;
    }else
    {
        self.playerItem = [AVPlayerItem playerItemWithURL:videoUrl];
        [[MJDownLoad shareInstanceManager]downLoadWithUrl:vedioUrl];
    }

最后对点击下载按钮的事件进行实现

#pragma Mark----downLoadBtn下载按钮点击事件
-(void)downLoadBtnPress:(UIButton*)sender
{
    if(!sender.selected)
    {
        [[MJDownLoad shareInstanceManager]downLoadWithUrl:vedioUrl];
    }else
    {
        sender.userInteractionEnabled = NO;
    }
}

下载功能实现完成,下一步实现下载列表,突然发现有一步做的多余了,不需要用到NSUserdefault,直接组装路径查看是否存在该路径即可知道是否存在缓存

未完待续--------------------------------------------------------

github:
https://github.com/AlexMJ666/MJPlayer

你可能感兴趣的