gpt4 book ai didi

ios - 通过 AVAudioMix 导出带有淡入淡出的 AVAsset

转载 作者:行者123 更新时间:2023-11-28 21:51:40 28 4
gpt4 key购买 nike

任务是修剪音频 Assets 并制作淡入淡出效果。这是我的代码:

+ (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];

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

enter image description here

最佳答案

我找到了解决方案。我不应该为淡入和淡出创建不同的 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]];

关于ios - 通过 AVAudioMix 导出带有淡入淡出的 AVAsset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28082835/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com