在iOS中使用AVAssetExportSession实现音频淡入淡出效果

3

我使用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]);
}
}

您想在音频中增加或减少某个时间范围内的音量吗? - Ketan Parmar
是的,我确实想要这个。 - Neha Purwar
请等一下,@NehaPurwar,我正在写。 - Jagveer Singh
好的 @JagveerSingh - Neha Purwar
2个回答

3
//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:^{ }]; 之前添加这几行代码:


感谢 @Jagveer Singh - Neha Purwar
不明白你的查询,请解释一下。? - Jagveer Singh
实际上,我已经在修剪过的视频上尝试过这个方法,效果完全符合我的要求。但是当我将它应用到我的原始音频上时,在播放音频的一部分之后,才会出现这种效果,而不是从音频开头开始。 - Neha Purwar
//假设音频长度为10秒,你想在其结束前将音量降低1.5浮点数长度=10,开始=lenth-1.5;[exportAudioMixInputParameters setVolume:1.0 atTime:kCMTimeZero]; [exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start, 1)]; exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters]; exportSession.audioMix = exportAudioMix; - Jagveer Singh
这是我的代码http://dpaste.com/3GXZNSC,我已经按照您的建议在Swift中实现了它,但它没有平滑地淡出。@JagveerSingh - Vivek Goswami
显示剩余3条评论

0

当播放音频时,您可以简单地使用if语句,通过使用nstimer,如果时间到达特定秒数,则增加音量,如果到达峰值秒数,则将音量降低到初始音量。

请参考此链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接