在将视频与原始音频合并时,我使用了以下代码。它一直有效,直到我升级到iOS9。任何人都面临相同的问题,解决任何帮助将不胜感激。经过一整天的研究,我什么都找不到。

AVAssetTrack *videoTrack = nil;
AVAssetTrack *audioTrack = nil;
CMTime insertionPoint = kCMTimeZero;

if([[url tracksWithMediaType:AVMediaTypeVideo] count] != 0) {
    videoTrack = [url tracksWithMediaType:AVMediaTypeVideo][0];
}

if([[url tracksWithMediaType:AVMediaTypeAudio] count] != 0) {
    audioTrack = [url tracksWithMediaType:AVMediaTypeAudio][0];
}

// Insert the video and audio tracks from AVAsset
if (videoTrack != nil) {
    AVMutableCompositionTrack *compositionVideoTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration]) ofTrack:videoTrack atTime:insertionPoint error:&error];
}
if (audioTrack != nil) {
    AVMutableCompositionTrack *compositionAudioTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration]) ofTrack:audioTrack atTime:insertionPoint error:&error];
}

最佳答案

发现了问题:
我在上面的代码之后使用了下面的代码。删除后,它工作正常。下面的额外行删除了iOS 9中已经添加的音轨。
希望它能帮助到某人

AVMutableCompositionTrack * compositionAudioTrack2 = [videoComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

10-01 08:05