次のコードを使用して複数を連結していますAVURLAssets
:
AVMutableComposition * movie = [AVMutableComposition composition];
CMTime offset = kCMTimeZero;
for (AVURLAsset * asset in assets) {
AVMutableCompositionTrack *compositionVideoTrack = [movie addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [movie addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
AVAssetTrack *assetAudioTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
NSError * error = nil;
if (![compositionVideoTrack insertTimeRange: timeRange ofTrack: assetVideoTrack atTime: offset error: &error]) {
NSLog(@"Error adding video track - %@", error);
}
if (![compositionAudioTrack insertTimeRange: timeRange ofTrack: assetAudioTrack atTime: offset error: &error]) {
NSLog(@"Error adding audio track - %@", error);
}
offset = CMTimeAdd(offset, asset.duration);
}
結果として得られるコンポジションは、すべての元のアセットの合計デュレーションまで再生され、オーディオは正しく再生されますが、最初のアセットのビデオのみが再生され、最終フレームで一時停止します。
私が間違ったことについて何か考えはありますか?
元のアセットの順序は関係ありません。最初のビデオとすべてのオーディオが再生されます。