音频中的淡入淡出效果通过在 ios 中使用 AVAssetExportSession

Fade In, Fade Out effect in audio By using AVAssetExportSession in ios

我已经使用 AVAssetExportSession 修剪了特定持续时间的音频,我也得到了修剪后的音频。

但我的问题是我想在我的音频中添加淡入淡出效果。

告诉我,我该如何解决这个问题? 任何帮助将不胜感激。

修剪音频的代码在这里--

- (void)trimAudio:(NSString *)inputAudioPath audioStartTime:(float)sTime audioEndTime:(float)eTime outputPath:(NSString *)outputFilePath mode:(NSInteger)kSelectionMode
{
@try
{
    AVAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:inputAudioPath] options:nil];

    //Create the session with assets
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];

    //Set the output url
    exportSession.outputURL = [NSURL fileURLWithPath:outputFilePath];

    // Trim video in a particular duration
    CMTime startTime = CMTimeMake((int)(floor(sTime * 100)), 100);
    CMTime stopTime = CMTimeMake((int)(ceil(eTime * 100)), 100);
    CMTimeRange range = CMTimeRangeFromTimeToTime(startTime, stopTime);
    exportSession.timeRange = range;

    exportSession.outputFileType = AVFileTypeAppleM4A;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch (exportSession.status) {
            case AVAssetExportSessionStatusCompleted:{
                NSLog(@"Export Complete");
                if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(exportSession.outputURL.path)){

                    UISaveVideoAtPathToSavedPhotosAlbum(exportSession.outputURL.path, nil, nil, nil);
                    if ([self.delegate respondsToSelector:@selector(trimDidSucceed:mode:)]) {
                        [self.delegate trimDidSucceed:outputFilePath mode:kTrimAudio];
                    }
                    else{
                        [self.delegate trimDidFail:exportSession.error];
                    }
                }

                break;
            }
            case AVAssetExportSessionStatusFailed:{
                NSLog(@"Export Error: %@", [exportSession.error description]);

                break;
            }
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export Cancelled");
                break;
            default:
                break;
        }
    }];


    exportSession = nil;

}
@catch (NSException * e)
{
    NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]);
}
}

您可以在播放该音频时简单地放置 if 语句,方法是 nstimer 如果时间到达特定秒数则增加音量,如果时间到达峰值秒数则将音量降低至初始音量。

检查 this link 以供参考。

//fade in /out

AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:asset.tracks.lastObject];

int start=2,length=3;

[exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start-1, 1)];
[exportAudioMixInputParameters setVolume:0.1 atTime:CMTimeMakeWithSeconds(start, 1)];
[exportAudioMixInputParameters setVolume:0.5 atTime:CMTimeMakeWithSeconds(start+1, 1)];
[exportAudioMixInputParameters setVolume:1.0 atTime:CMTimeMakeWithSeconds(start+2, 1)];



[exportAudioMixInputParameters setVolume:1.0 atTime:CMTimeMakeWithSeconds((start+length-2), 1)];
[exportAudioMixInputParameters setVolume:0.5 atTime:CMTimeMakeWithSeconds((start+length-1), 1)];
[exportAudioMixInputParameters setVolume:0.1 atTime:CMTimeMakeWithSeconds((start+length), 1)];

exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters];

exportSession.audioMix = exportAudioMix; // fade in audio mix

只需在 [exportSession exportAsynchronouslyWithCompletionHandler:^{ }];