3

I have trimmed an audio for a particular duration by using AVAssetExportSession and i am also getting the trimmed audio.

But my problem is that i want to add fade in and fade out effect in my audio.

Let me know, how can i solve this? Any help will be appreciated.

Code for trimming audio is here--

- (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]);
}
}
Neha Purwar
  • 175
  • 1
  • 13

2 Answers2

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

Just add these few line before [exportSession exportAsynchronouslyWithCompletionHandler:^{ }];

Jagveer Singh
  • 2,258
  • 19
  • 34
  • Thanks @Jagveer Singh – Neha Purwar Apr 21 '16 at 07:18
  • When i want to add fade in out effect in a original audio, its adding the fade in effect after some duration of time. Can anyone resolve this?? – Neha Purwar Apr 21 '16 at 10:16
  • dont get ur query, please explain . ? – Jagveer Singh Apr 21 '16 at 11:29
  • Actually , i tried this on the trimmed video and its working exactly as i want but when i am applying this with my original audio , i am getting this effect after playing a part of audio rather than starting of audio. – Neha Purwar Apr 21 '16 at 12:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109810/discussion-between-jagveer-singh-and-neha-purwar). – Jagveer Singh Apr 21 '16 at 12:43
  • if i want to just fade out for last 1.5 second so what should i change in this code? @JagveerSingh – Vivek Goswami Jun 14 '17 at 15:30
  • //Suppose audio length Is 10 seconds , you want to reduce volume 1.5 before its ends Float length= 10, start=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 Jun 15 '17 at 05:49
  • Here is my code http://dpaste.com/3GXZNSC which i have implement in Swift as per your suggestion but it not fading smoothly. @JagveerSingh – Vivek Goswami Jun 15 '17 at 06:29
0

You can simply put if statement when playing that audio by taking nstimer like if time reach at particular second that increase volume and if it reach at peak second then decrease volume to initial volume.

Check this link for reference.

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75