2016-04-21 30 views
3

我已使用AVAssetExportSession裁剪了特定持續時間的音頻,並且我還獲得了裁剪後的音頻。淡入淡出在音頻中的效果通過在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]); 
} 
} 
+0

你想增加或減少音量的一段時間的音量? – Lion

+0

是的,我確實需要這個。 –

+0

pleae w8 @NehaPurwar我正在寫作 –

回答

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:^ { }];

+0

你試過了嗎? –

+0

是的,我試了這個,它的工作完美:) –

+0

謝謝@Jagveer辛格 –

0

採取nstimer喜歡在特定第二是提高音量,如果時間達到播放音頻時,您可以簡單地把if語句,如果它在達到高峯,然後第二將音量降至初始音量。

查詢this link僅供參考。