任务是修剪音频资产并产生淡入淡出效果。这是我的代码:

+ (void)exportAsset:(AVAsset *)asset trimTimeRange:(CMTimeRange)trimTimeRange fadeDuration:(CMTime)fadeDuration
{
    NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
    AVAssetTrack *track = [tracks objectAtIndex:0];

    CMTimeRange fadeInTimeRange = CMTimeRangeMake(trimTimeRange.start, fadeDuration);

    AVMutableAudioMixInputParameters *exportAudioMixFadeInInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
    [exportAudioMixFadeInInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];

    CMTime startFadeOutTime = CMTimeMake(trimTimeRange.start.value + trimTimeRange.duration.value - fadeDuration.value, 1000);
    CMTimeRange fadeOutTimeRange = CMTimeRangeMake(startFadeOutTime, fadeDuration);

    AVMutableAudioMixInputParameters *exportAudioMixFadeOutInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
    [exportAudioMixFadeOutInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];

    AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
    exportAudioMix.inputParameters = @[exportAudioMixFadeOutInputParameters, exportAudioMixFadeInInputParameters];

    NSString *path = [NSString stringWithFormat:@"%@/%@", [PathHelper applicationDocumentsDirectory], filenameM4A];

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
    exportSession.audioMix = exportAudioMix;
    exportSession.outputURL = [NSURL fileURLWithPath:path];
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.timeRange = trimTimeRange;

    [exportSession exportAsynchronouslyWithCompletionHandler:nil];
}


这很奇怪,但是如果我像这样传递音频混合的输入参数

exportAudioMix.inputParameters = @[exportAudioMixFadeInInputParameters, exportAudioMixFadeOutInputParameters];


我得到第一个结果(看图片)
如果这样

exportAudioMix.inputParameters = @[exportAudioMixFadeOutInputParameters, exportAudioMixFadeInInputParameters];


我得到第二个结果。
看起来第二个参数正在忽略。谁能解释这种行为?

最佳答案

我找到了解决方案。我不应该为淡入和淡出创建AVMutableAudioMixInputParameters的其他项目。这是我的代码:

CMTimeRange fadeInTimeRange = CMTimeRangeMake(trimTimeRange.start, fadeDuration);

CMTime startFadeOutTime = CMTimeMake(trimTimeRange.start.value + trimTimeRange.duration.value - fadeDuration.value, 1000);
CMTimeRange fadeOutTimeRange = CMTimeRangeMake(startFadeOutTime, fadeDuration);

AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];
[exportAudioMixInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];

AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters]];

10-08 19:49