2012-04-16 78 views
6

我試圖將4個音軌合併爲一個合成,然後將此合成導出到文件。到目前爲止,我的文件已成功創建,但所有音軌都以全音量播放,而不是我試圖設置的音量。這裏就是我現在做的:在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:^{ ... }]; 

唯一的其他有趣的事情是audioParamsForTrack方法,它是在這裏:

- (AVAudioMixInputParameters *)audioParamsForTrack:(AVAssetTrack *)track volume:(float)vol{ 
    AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track]; 
    [audioInputParams setVolume:vol atTime:kCMTimeZero]; 

    return [audioInputParams copy]; 
} 

任何人能發現我在做什麼錯?我試過通過各種不同的音軌來創建音頻參數,但它似乎沒有什麼區別。我看到一些關於音軌首選音量的東西 - 這可能對我有幫助嗎?我有點卡住在這一點上,任何反饋意見是讚賞!

+0

這不一定是回答你的問題,但其相關的,你可以設置多個時間在一個AVMutableAudioMixInputParameter對象上。因此要在相同的軌道上設置一個斜坡,您需要在相同的參數實例上設置這些斜坡。我一開始並不明白,我認爲我需要爲每個音量斜坡添加一個參數對象。 – wbarksdale 2015-05-20 10:00:37

回答

1

調試時,我可以看到問題,但我不知道爲什麼會發生。

當您的PARAMS添加到數組,像這樣:

[inputParams addObject:audioParams]; 

道ID被設置爲0,因此從未接到任何曲目:

從控制檯:

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

我解決了這個問題,不使用audioParamsForTrack方法,而是在數組之前完成這項工作,如下所示:

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

我也有類似的問題,但什麼工作對我來說是明確設置輸入參數的軌道ID:

[audioInputParams setTrackID:compositionAudioTrack.trackID]; 
+1

這爲我做了詭計。只要確定您設置的trackId來自compositionAudioTrack,而不是AVAssetTrack對象(如果適用)。 – manderson 2016-04-30 19:06:59