iOS 9 : AVFoundation Export Session is missing audio

人盡茶涼 提交于 2020-01-03 03:48:05

问题


I'm using the below code snipped while merging videos with original audio. It has been working until I upgraded to iOS9. Anyone faced the same issue and any help to resolve would be greatly appreciated. I couldn't find anything after researching whole day.

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];
}

回答1:


Found the issue: I have used the below after the above code. After removing this it worked fine. The below extra line removes the already added audio track in iOS 9. Hope it helps someone!

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




回答2:


I have had a similar problem, but my observation is that you can not add an audio track with no audio anymore. IOS 9 will then remove the complete audio.

So only this line will remove all audio 
compositionAudioTrack = [videoComposition 
addMutableTrackWithMediaType:AVMediaTypeAudio 
preferredTrackID:kCMPersistentTrackID_Invalid];

if not followed with a valid

[compositionAudioTrack insertTimeRange:
     CMTimeRangeMake(kCMTimeZero, [url duration]) 
     ofTrack:audioTrack 
     atTime:insertionPoint error:&error];

So the test on audioTrack != nil is not enough.

I combine two audio tracks with one video with no problems anymore for IOS 9.



来源:https://stackoverflow.com/questions/32684968/ios-9-avfoundation-export-session-is-missing-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!