在AVMutableComposition上设置轨道级别

时间:2012-04-16 20:43:18

标签: iphone avfoundation

我正在尝试将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];
}

有人能发现我做错了吗?我已经尝试传递各种不同的音轨来创建音频参数,但它似乎没有什么区别。我看到了一首关于音轨首选音量的信息 - 这有什么可能对我有帮助吗?我在这一点上有点困惑,任何反馈都表示赞赏!

2 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,但对我有用的是在输入参数上明确设置轨道ID:

    [audioInputParams setTrackID:compositionAudioTrack.trackID];

答案 1 :(得分:1)

调试时我可以看到问题,但我不确定为什么会这样。

将参数添加到数组时,如下所示:

[inputParams addObject:audioParams];

track 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;