//----------边下边播—缓存视频---------
NSArray *loadedTimeRanges = [self.playerItem loadedTimeRanges];
CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval timeInterval = startSeconds + durationSeconds;// 计算缓冲总进度
CMTime duration = self.playerItem.duration;
CGFloat totalDuration = CMTimeGetSeconds(duration);
// NSLog(@"下载进度:%.2f %f %f", timeInterval / totalDuration,timeInterval,totalDuration);
CGFloat timeee = [[NSString stringWithFormat:@"%.3f",timeInterval] floatValue];
CGFloat totall = [[NSString stringWithFormat:@"%.3f",totalDuration] floatValue];
if (timeee >= totall) {
// NSLog(@“下载完成”);
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *erroraudio = nil;
//获取AVAsset中的音频 或 者视频
AVAssetTrack *assetAudioTrack = [[self.playerItem.asset tracksWithMediaType:AVMediaTypeAudio] firstObject];
//向通道内加入音频或者视频
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.playerItem.asset.duration)
ofTrack:assetAudioTrack
atTime:kCMTimeZero
error:&erroraudio];
NSError *errorVideo = nil;
AVAssetTrack *assetVideoTrack = [[self.playerItem.asset tracksWithMediaType:AVMediaTypeVideo]firstObject];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.playerItem.asset.duration)
ofTrack:assetVideoTrack
atTime:kCMTimeZero
error:&errorVideo];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetPassthrough];
// 4 - 输出路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths lastObject];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"myVideo.mp4"]];
//NSURL *videoPath = [NSURL fileURLWithPath:myPathDocs];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:myPathDocs];
// NSLog(@"—myPathDocs–%@-----",myPathDocs);
//存在就删除 一次只保留一个视频
if (blHave) {
[fileManager removeItemAtPath:myPathDocs error:nil];
// NSLog(@"存在就删除 一次只保留一个视频");
}
exporter.outputURL = [NSURL fileURLWithPath:myPathDocs];;
exporter.outputFileType = AVFileTypeMPEG4;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
if( exporter.status == AVAssetExportSessionStatusCompleted){
//保存到相册(如果要保存到相册,需要先确认项目是否允许访问相册)
//UISaveVideoAtPathToSavedPhotosAlbum(myPathDocs, nil, nil, nil);
// NSLog(@"保存成功 %@",myPathDocs);
// NSLog(@“保存成功”);
}else if( exporter.status == AVAssetExportSessionStatusFailed ){
// NSLog(@“保存失败”);
}
}];
}
//----------边下边播—缓存视频---------