你的位置:首页 > 操作系统

[操作系统]视频播放器边下边播(保存到沙盒,显示进度)


没怎么研究视频播放器,就在网上找了个,链接:https://github.com/835239104/KrVideoPlayerPlus

下载AFNetworking,

1.下载最新版的会报错,没有引入相关库

2.AFSecurityPolicy.m中:注释了一部分代码

#pragma mark - 这里改了 ------#warning - 这里改了 --------//#if !TARGET_OS_IOS && !TARGET_OS_WATCH//static NSData * AFSecKeyGetData(SecKeyRef key) {//  CFDataRef data = NULL;////  __Require_noErr_Quiet(SecItemExport(key, kSecFormatUnknown, kSecItemPemArmour, NULL, &data), _out);////  return (__bridge_transfer NSData *)data;////_out://  if (data) {//    CFRelease(data);//  }////  return nil;//}//#endifstatic BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) {//#if TARGET_OS_IOS || TARGET_OS_WATCH  return [(__bridge id)key1 isEqual:(__bridge id)key2];//#else//  return [AFSecKeyGetData(key1) isEqual:AFSecKeyGetData(key2)];//#endif}

3.实现边下边播(我用的是一边在网上看一边下载,所以会有点卡)

/** * 下载文件 */- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag{  NSFileManager *fileManager = [NSFileManager defaultManager];    //检查本地文件是否已存在  NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName];    //检查附件是否存在  if ([fileManager fileExistsAtPath:fileName]) {     [self addVideoPlayerWithURL:[NSURL fileURLWithPath:fileName]];  }else{    //创建附件存储目录    if (![fileManager fileExistsAtPath:aSavePath]) {      [fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil];    }//     [self addVideoPlayerWithURL:[NSURL fileURLWithPath:fileName]];    [self addVideoPlayerWithURL:[NSURL URLWithString:aUrl]];    //下载附件    NSURL *url = [[NSURL alloc] initWithString:aUrl];    NSURLRequest *request = [NSURLRequest requestWithURL:url];        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];    operation.inputStream  = [NSInputStream inputStreamWithURL:url];    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];        //下载进度控制         [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {     NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead);     }];        //已完成下载    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {//      NSData *audioData = [NSData dataWithContentsOfFile:fileName];      NSLog(@"完成下载");      //设置下载数据到res字典对象中并用代理返回下载数据NSData    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {      NSLog(@"下载失败");      //下载失败    }];        [operation start];  }}

注意:从本度读取路径:[NSURL fileURLWithPath:fileName]

在网上读取路径:[NSURL URLWithString:aUrl]

Demo百度云下载链接:http://pan.baidu.com/s/1c0bL9dQ