Tôi đang cố gắng kết hợp 4 bản âm thanh vào một sáng tác, sau đó xuất thành phần này thành tệp. Cho đến nay tệp của tôi được tạo thành công nhưng tất cả các bản âm thanh phát lại ở mức âm lượng lớn thay vì các mức âm lượng tôi đang cố gắng đặt. Dưới đây là những gì tôi đang làm bây giờ:Đặt mức bản nhạc trên AVMutableComposition
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:^{ ... }];
Các chỉ điều thú vị khác là phương pháp audioParamsForTrack, mà là ở đây:
- (AVAudioMixInputParameters *)audioParamsForTrack:(AVAssetTrack *)track volume:(float)vol{
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[audioInputParams setVolume:vol atTime:kCMTimeZero];
return [audioInputParams copy];
}
bất cứ ai có thể nhận ra những gì tôi đang làm sai? Tôi đã thử vượt qua tất cả các loại bài hát khác nhau để tạo ra các thông số âm thanh nhưng dường như nó không tạo ra sự khác biệt. Tôi thấy điều gì đó về khối lượng ưa thích của một bản nhạc - có phải điều này có thể giúp tôi không? Tôi là một chút bị mắc kẹt vào thời điểm này, bất kỳ thông tin phản hồi được đánh giá cao!
này không hẳn là một câu trả lời cho câu hỏi của bạn nhưng có liên quan của nó, bạn có thể đặt nhiều lần trên một đối tượng AVMutableAudioMixInputParameter duy nhất. do đó, để thiết lập một đoạn đường nối lên xuống trên cùng một rãnh, bạn cần phải thiết lập các đường dốc đó trên cùng một cá thể tham số. Tôi không hiểu điều đó lúc đầu và nghĩ rằng tôi cần phải thêm một đối tượng Tham số trên mỗi đoạn đường. – wbarksdale