7

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

谁能发现我做错了什么?我尝试通过各种不同的音轨来创建音频参数,但似乎没有什么不同。我看到了一些关于曲目首选音量的信息 - 这对我有帮助吗?我在这一点上有点卡住了,感谢任何反馈!

4

2 回答 2

3

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

    [audioInputParams setTrackID:compositionAudioTrack.trackID];
于 2015-01-23T04:07:43.087 回答
1

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

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

[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;
于 2013-11-27T18:44:55.327 回答