Я пытаюсь объединить 4 звуковые дорожки в одну композицию, а затем экспортировать эту композицию в файл. Пока что мой файл создан успешно, но все звуковые дорожки воспроизводятся с полной громкостью, а не с теми уровнями громкости, которые я пытаюсь установить. Вот что я сейчас делаю:
AVMutableComposition *trackComposition = [AVMutableComposition composition];
AVAsset *asset1 =...
AVAsset *asset2 =...
AVAsset *asset3 =...
AVAsset *asset4 =...
NSMutableArray *inputParams = [NSMutableArray arrayWithCapacity:4];
// Add 4 tracks to composition (but only if there are no errors and the track isn't muted
NSError *err;
if(asset1 && ![self trackIsMuted:1]){
AVAssetTrack *rawTrack = [[asset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset1 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:1]];
[inputParams addObject:audioParams];
}
if(asset2 && !err && ![self trackIsMuted:2]){
AVAssetTrack *rawTrack = [[asset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset2 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:2]];
[inputParams addObject:audioParams];
}
if(asset3 && !err && ![self trackIsMuted:3]){
AVAssetTrack *rawTrack = [[asset3 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset3 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:3]];
[inputParams addObject:audioParams];
}
if(asset4 && !err && ![self trackIsMuted:4]){
AVAssetTrack *rawTrack = [[asset4 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset4 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:4]];
[inputParams addObject:audioParams];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = inputParams;
// Export the composition to a file
AVAssetExportSession *export = [AVAssetExportSession exportSessionWithAsset:trackComposition presetName:AVAssetExportPresetAppleM4A];
NSURL *outputURL = [NSURL fileURLWithPath:[[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString guidString]] stringByAppendingPathExtension:@"m4a"]];
[export setOutputURL:outputURL];
[export setOutputFileType:@"com.apple.m4a-audio"];
[export setAudioMix:audioMix];
[export exportAsynchronouslyWithCompletionHandler:^{... }];
Еще одна интересная вещь — это метод audioParamsForTrack, который находится здесь:
- (AVAudioMixInputParameters *)audioParamsForTrack:(AVAssetTrack *)track volume:(float)vol{
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[audioInputParams setVolume:vol atTime:kCMTimeZero];
return [audioInputParams copy];
}
Кто-нибудь может заметить, что я делаю неправильно? Я пробовал передавать все виды разных дорожек для создания параметров звука, но, похоже, это не имеет значения. Я видел кое-что о предпочтительной громкости трека -. Это может мне помочь? Я немного застрял на этом этапе, любые отзывы приветствуются!