2012-04-16 27 views
6

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!

+0

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

Trả lời

1

Khi gỡ lỗi này, tôi có thể thấy vấn đề, nhưng tôi không chắc tại sao nó lại xảy ra.

Khi bạn thêm các params để mảng, như thế này:

[inputParams addObject:audioParams]; 

Ca khúc id được thiết lập là 0, do đó nó không bao giờ gắn với bất kỳ ca khúc:

Từ giao diện điều khiển:

<AVMutableAudioMixInputParameters: 0x16eab300, track ID = 0, volume mix: volume 0.010 at time 0.000> 

Tôi đã giải quyết vấn đề này bằng cách không sử dụng phương thức audioParamsForTrack, nhưng thực hiện công việc ngay trước mảng, như sau:

NSMutableArray *inputParams = [NSMutableArray arrayWithCapacity:4]; 

      AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:AudioTrack]; 
      [audioInputParams setVolume:.01f atTime:kCMTimeZero]; 

      AVAudioMixInputParameters *audioParams = audioInputParams; 
[inputParams addObject:audioParams]; 

      audioMix.inputParameters = inputParams; 
2

Tôi đã có một vấn đề tương tự nhưng những gì làm việc cho tôi là thiết lập Id theo dõi một cách rõ ràng trên các thông số đầu vào:

[audioInputParams setTrackID:compositionAudioTrack.trackID]; 
+1

Điều này đã làm cho các trick cho tôi. Chỉ cần chắc chắn rằng bạn đang thiết lập trackId đến từ compositionAudioTrack, KHÔNG phải đối tượng AVAssetTrack nếu có. – manderson

Các vấn đề liên quan