思路
使用AVMutableComposition組合拼接出想要的分段組合方式,
反復(fù)播放通常要處理三個(gè)分段,1.重復(fù)之前的直接播放段 2.反復(fù)段 3.反復(fù)段之后的直接播放段.
邏輯比較簡(jiǎn)單 直接上代碼
- (void)videoSectionReplay {
//測(cè)試數(shù)據(jù)
CMTimeRange replayTimeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(3, 600), CMTimeMakeWithSeconds(3, 600));
NSUInteger replayCount = 3;
//測(cè)試資源
NSString *filePath4 = [[NSBundle mainBundle] pathForResource:@"timeMerge" ofType:@"MP4"];
AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:filePath4] options:@{}];
AVAssetTrack *videoTrack = asset.videoTrack;
AVAssetTrack *audioTrack = asset.audioTrack;
//要重復(fù)的時(shí)間段 在視頻軌道中就沒有 無法繼續(xù)處理直接返回
if (!CMTimeRangeContainsTimeRange(videoTrack.timeRange, replayTimeRange)) {
return;
}
//組合對(duì)象
AVMutableComposition *mutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime videoInsertOffset = kCMTimeZero;
CMTime audioInsertOffset = kCMTimeZero;
//如果有正常播放的前段 插入正常播放的前段
if (CMTIME_COMPARE_INLINE(replayTimeRange.start, !=, kCMTimeZero)) {
CMTime duration = CMTimeSubtract(replayTimeRange.start, videoTrack.timeRange.start);
CMTimeRange prePassThroughTimeRange = CMTimeRangeMake(videoTrack.timeRange.start, duration);
[videoCompositionTrack insertTimeRange:prePassThroughTimeRange ofTrack:videoTrack atTime:videoInsertOffset error:nil];
[audioCompositionTrack insertTimeRange:prePassThroughTimeRange ofTrack:audioTrack atTime:audioInsertOffset error:nil];
videoInsertOffset = videoCompositionTrack.timeRange.duration;
audioInsertOffset = audioCompositionTrack.timeRange.duration;
}
//插入反復(fù)播放的片段
for (NSUInteger i=0; i<replayCount; i++) {
[videoCompositionTrack insertTimeRange:replayTimeRange ofTrack:videoTrack atTime:videoInsertOffset error:nil];
[audioCompositionTrack insertTimeRange:replayTimeRange ofTrack:audioTrack atTime:audioInsertOffset error:nil];
videoInsertOffset = videoCompositionTrack.timeRange.duration;
audioInsertOffset = audioCompositionTrack.timeRange.duration;
}
//如果后面還有需要直接播放的片段 插入后面的直接播放的段
CMTime replayEndTime = CMTimeAdd(replayTimeRange.start, replayTimeRange.duration);
if (CMTIME_COMPARE_INLINE(videoTrack.timeRange.duration, >, replayEndTime)) {
CMTime duration = CMTimeSubtract(videoTrack.timeRange.duration, replayEndTime);
CMTimeRange backTimeRange = CMTimeRangeMake(replayEndTime, duration);
[videoCompositionTrack insertTimeRange:backTimeRange ofTrack:videoTrack atTime:videoInsertOffset error:nil];
[audioCompositionTrack insertTimeRange:backTimeRange ofTrack:audioTrack atTime:audioInsertOffset error:nil];
videoInsertOffset = videoCompositionTrack.timeRange.duration;
audioInsertOffset = audioCompositionTrack.timeRange.duration;
}
combineResultTestViewController *vc = [[combineResultTestViewController alloc] initWithAsset:mutableComposition];
[self.navigationController pushViewController:vc animated:YES];
}
還用到了一個(gè)簡(jiǎn)單的擴(kuò)展
@implementation AVAsset (AHSVAsset)
- (AVAssetTrack *)videoTrack {
AVAssetTrack *videoTrack = [self tracksWithMediaType:AVMediaTypeVideo].firstObject;
return videoTrack;
}
- (AVAssetTrack *)audioTrack {
AVAssetTrack *audioTrack = [self tracksWithMediaType:AVMediaTypeAudio].firstObject;
return audioTrack;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者