Reputation: 3823
I have been trying different approaches to this problem, but they take too long to process (alter the MP3 file for different volumes).
I have an AVMutableComposition that gets filled with several AVMutableCompositionTrack for audio and video. The mixing works fine but the adjustment in the volume for the audio track is not working and fails when exporting.
Here is the code I use:
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVURLAsset *soundTrackAsset = [[AVURLAsset alloc]initWithURL:trackTempProcessedURL options:nil];
//ADDING AUDIO
AVMutableCompositionTrack *compositionAudioSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:trackIDSoundTrack];
[compositionAudioSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[soundTrackAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:CMTimeAdd(cmTimeDifference,startTime) error:nil];
NSArray *tracksToDuck = [mixComposition tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
[trackMix setVolume:volume atTime:kCMTimeZero];
[trackMixArray addObject:trackMix];
}
audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = trackMixArray;
//ADDING VIDEO
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:frontAssetURL options:nil];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:startTime error:nil];
//EXPORTING
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName: AVAssetExportPresetPassthrough];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
_assetExport.audioMix = audioMix;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
...
Everything mixes fine without the audio mixer but when I try to alter the volume the export gives me an error:
AVFoundationErrorDomain Error: 11822
Upvotes: 1
Views: 3910
Reputation: 1126
AVMutableAudioMixInputParameters need set the "trackID" to indicates which audio track should applied the parameters.
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
[trackMix setVolume:volume atTime:kCMTimeZero];
//+++++code
AVMutableCompositionTrack * track = [tracksToDuck objectAtIndex:i]
[trackMix setTrackID:[track trackID]];
[trackMixArray addObject:trackMix];
}
Upvotes: 2