美文网首页iOS Developer
简单实现判断本地是否存在已经下载视频

简单实现判断本地是否存在已经下载视频

作者: 静花寒 | 来源:发表于2016-01-09 19:49 被阅读222次

    前言:最近笔者在做视频播放器,所以,写的东西,都和这个有些关系,希望对大家有所帮助。废话不多说,进入主题。

    1.首先播放页面需要写个属性接收本地的FileName,然后传递链接的时候做判断,这样第一步就完成了

    NSURL *playURL;
        if (self.playName != nil) {
            playURL = [NSURL fileURLWithPath:self.playName];
        }else{
            playURL = [NSURL URLWithString:self.dailySelected.playUrl];
        }
    

    2.关于传递的FileName,是一个放在数据库的全部不变量,所以我们必须为此建一个表,并且新建一个Model。如果不是不变量,会存在退出后,在点相同页面时,传值丢失问题

    //下载按钮
        [self.playView.downloadButton addTarget:self action:@selector(downloadMovie) forControlEvents:(UIControlEventTouchUpInside)];
    
    - (void)downloadMovie
    {
        
        self.downloadPercent = [[ProgressView alloc] initWithURL:[NSURL URLWithString:self.dailySelected.playUrl] progressViewWithFrame:(CGRectMake(self.playView.downloadButton.frame.origin.x + 30, self.playView.downloadButton.frame.origin.y + self.navigationController.navigationBar.frame.size.height + 20 + 10, 60, 20)) font:[UIFont fontWithName:@"Georgia-BoldItalic" size:12.0] color:[UIColor whiteColor] timeout:5.0 alive:YES radius:0 delegate:self];
        [self.view addSubview:self.downloadPercent];
        self.playView.downloadButton.userInteractionEnabled = NO;
    }
    
    - (void)progressView:(ProgressView *)progressView didFinishedWithData:(NSData *)data suggestedWithFileName:(NSString *)fileName
    {
        [UIView animateWithDuration:0.5 animations:^{
            self.downloadPercent.alpha = 0;
        }];
        [self.downloadPercent performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
        self.playView.downloadButton.tintColor = [UIColor redColor];
        self.playView.downloadButton.userInteractionEnabled = NO;
        [[OpenEyeDataBase shareSqliteManager]insertDownloadWithModel:self.dailySelected];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"downloadComplete" object:nil];
        self.fileName = fileName;
        //将路径插入数据库
        [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected fileName:fileName];
        //NSLog(@"%@",self.fileName);
    }
    

    注:上面代码有个小封装,在我另一个文章中有源码

    3.最后就是在进入页面时,有个判断,通过遍历数据库来判断

    - (void)viewWillAppear:(BOOL)animated
    {
        for (DailySelected *dailySelected in [[OpenEyeDataBase shareSqliteManager] selectDownloadAllModel]) {
            NSString *string = [NSString stringWithFormat:@"%@",self.dailySelected.ID];
            if (string == dailySelected.ID) {
                self.playView.downloadButton.tintColor = [UIColor redColor];
                self.playView.downloadButton.userInteractionEnabled = NO;
                for (FileModel *fileModel in [[OpenEyeDataBase shareSqliteManager] selectDownloadFileNameAllModel]) {
                    if (string == fileModel.ID) {
                        self.fileName = fileModel.fileName;
                    }
                }
            }
        }
    }
    

    这些操作后,就能实现了,如果不明白可以留言,小弟一定详细及及时解释

    相关文章

      网友评论

        本文标题:简单实现判断本地是否存在已经下载视频

        本文链接:https://www.haomeiwen.com/subject/eikqkttx.html