ios - 裁剪音频文件

标签 ios objective-c audio avaudioplayer

我想在 iOS 中实现音频修剪,我想要一个双向 slider 之类的东西,用户可以在录制音频的特定时间段上滑动,然后我将保存该音频,如下图 enter image description here

最佳答案

AVAssetExportSession - 导出经过修剪的音频 Assets

以下代码将为您完成工作,请参阅此 link更多详情

- (BOOL)exportAsset:(AVAsset *)avAsset toFilePath:(NSString *)filePath {

    // we need the audio asset to be at least 50 seconds long for this snippet
    CMTime assetTime = [avAsset duration];
    Float64 duration = CMTimeGetSeconds(assetTime);
    if (duration < 50.0) return NO;

    // get the first audio track
    NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
    if ([tracks count] == 0) return NO;

    AVAssetTrack *track = [tracks objectAtIndex:0];

    // create the export session
    // no need for a retain here, the session will be retained by the
    // completion handler since it is referenced there
    AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset:avAsset
                                           presetName:AVAssetExportPresetAppleM4A];
    if (nil == exportSession) return NO;

    // create trim time range - 20 seconds starting from 30 seconds into the asset
    CMTime startTime = CMTimeMake(30, 1);
    CMTime stopTime = CMTimeMake(50, 1);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    // create fade in time range - 10 seconds starting at the beginning of trimmed asset
    CMTime startFadeInTime = startTime;
    CMTime endFadeInTime = CMTimeMake(40, 1);
    CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime,
                                                            endFadeInTime);

    // setup audio mix
    AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
    AVMutableAudioMixInputParameters *exportAudioMixInputParameters =
            [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

    [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0
                                   timeRange:fadeInTimeRange]; 
    exportAudioMix.inputParameters = [NSArray
                                      arrayWithObject:exportAudioMixInputParameters]; 

    // configure export session  output with all our parameters
    exportSession.outputURL = [NSURL fileURLWithPath:filePath]; // output path
    exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
    exportSession.timeRange = exportTimeRange; // trim time range
    exportSession.audioMix = exportAudioMix; // fade in audio mix

    // perform the export
    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (AVAssetExportSessionStatusCompleted == exportSession.status) {
            NSLog(@"AVAssetExportSessionStatusCompleted");
        } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
            // a failure may happen because of an event out of your control
            // for example, an interruption like a phone call comming in
            // make sure and handle this case appropriately
            NSLog(@"AVAssetExportSessionStatusFailed");
        } else {
            NSLog(@"Export Session Status: %d", exportSession.status);
        }
    }];

    return YES;
}

关于ios - 裁剪音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39166691/

相关文章:

ios - 低内存警告总是导致 iPad(第一代)崩溃

ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath

objective-c - segue后tableview上方的黑条

ios - Xcode 5 无法识别 Storyboard中的自定义 UIViewController 子类

HTML 5 音频、视频不工作 Safari 5.1 WinXP

ios - var label : UILabel! 和 var label = UILabel( ) 有什么区别?

ios - 如何增加和减少 TableViewCell 中自定义单元格的高度

batch-file - 尝试使用 ffmpeg 将 wav 与视频文件合并

python - Python中的音频处理

ios - 滚动条没有到达 tableview 的末尾