I have worked on a similar project before, and I am pulling this code from there. In this project, I combine between 1 and 160 sounds one by one, and this code works.
AVMutableComposition *composition = [AVMutableComposition composition];
NSMutableArray* audioMixParams = [[NSMutableArray alloc] init];
AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[self setUpAndAddAudioAtPath:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:test1 ofType:@"m4a"]] toComposition:composition atStartTime:[composition duration];
[self setUpAndAddAudioAtPath:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:test2 ofType:@"m4a"]] toComposition:composition atStartTime:[composition duration];
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[trackMix setVolume:0.8f atTime:CMTimeMake(0, 1)];
[audioMixParams addObject:trackMix];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams];
NSLog (@"compatible presets for songAsset: %@",
[AVAssetExportSession exportPresetsCompatibleWithAsset:composition]);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: composition
presetName: AVAssetExportPresetAppleM4A];
exporter.audioMix = audioMix;
exporter.outputFileType = @"com.apple.m4a-audio";
NSURL *exportURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/test.m4a",docDir]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[docDir stringByAppendingPathComponent:@"test.m4a"] error:NULL];
exporter.outputURL = exportURL;
[exporter exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
case AVAssetExportSessionStatusCompleted: {
NSLog (@"AVAssetExportSessionStatusCompleted");
break;
}
case AVAssetExportSessionStatusUnknown: NSLog (@"AVAssetExportSessionStatusUnknown"); break;
case AVAssetExportSessionStatusExporting: {
dispatch_async(dispatch_get_main_queue(), ^{
});
NSLog (@"AVAssetExportSessionStatusExporting");
break;
}
case AVAssetExportSessionStatusCancelled: NSLog (@"AVAssetExportSessionStatusCancelled"); break;
case AVAssetExportSessionStatusWaiting: NSLog (@"AVAssetExportSessionStatusWaiting"); break;
default: NSLog (@"didn't get export status"); break;
}
}];
, :
- (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition atStartTime: (CMTime) startTime{
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
CMTime trackDuration = songAsset.duration;
AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
NSError *error = nil;
BOOL ok = NO;
CMTimeRange tRange = CMTimeRangeMake(CMTimeMake(0, 1), trackDuration);
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[trackMix setVolume:0.8f atTime:startTime];
[self.audioMixParams addObject:trackMix];
ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:startTime error:&error];
}
, AS-IS. , , .