2012-02-22 56 views
1

在用牆壁敲擊我的頭幾次並嘗試不同的方法需要很長時間來處理(更改不同音量的MP3文件)後,我決定將此問題發佈給所有人希望找到有答​​案的人。如何更改AVMutableComposition中音軌的音量

我有一個AVMutableComposition獲取音頻和視頻的幾個AVMutableCompositionTrack。混音工作正常,但音軌的音量調整不起作用,並在導出時失敗。

這裏是我使用的代碼:

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) { 
... 

一切混合無需音頻混頻器正常,但當我試圖改變卷出口給我一個錯誤:

AVFoundationErrorDomain Error: 11822 
+0

我知道它與黑客工作。將音頻文件合併在一起而不需要視頻,然後在視頻中進行第二遍合併。我希望有更好的辦法。 – Dex 2012-03-07 04:54:33

+0

謝謝德克斯我會嘗試。 – 2012-03-08 16:03:12

+0

你有沒有得到這個工作..? – 2012-05-03 07:58:32

回答

1

AVMutableAudioMixInputParameters需要設置用於指示哪個音軌應該應用參數的「trackID」。

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]; 
} 
+0

這是行不通的 – 2017-08-08 11:08:13