iOS 边下边播
//----------邊下邊播—緩存視頻---------
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(@“下載完成”);
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];NSError *erroraudio = nil;//獲取AVAsset中的音頻 或 者視頻AVAssetTrack *assetAudioTrack = [[self.playerItem.asset tracksWithMediaType:AVMediaTypeAudio] firstObject];//向通道內加入音頻或者視頻[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.playerItem.asset.duration)ofTrack:assetAudioTrackatTime:kCMTimeZeroerror:&erroraudio];NSError *errorVideo = nil;AVAssetTrack *assetVideoTrack = [[self.playerItem.asset tracksWithMediaType:AVMediaTypeVideo]firstObject];[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.playerItem.asset.duration)ofTrack:assetVideoTrackatTime:kCMTimeZeroerror:&errorVideo];AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixCompositionpresetName: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(@“保存失敗”);
}
}];
}
//----------邊下邊播—緩存視頻---------
總結
- 上一篇: vs2017 错误:当前页面的脚本发生错
- 下一篇: 鼠标右键失灵怎么办