iOS音频修剪

Muh*_*ama 10 audio trim crop seek ios

我搜索了很多,找不到任何相关的...我正在研究iOS音频文件,这就是我想要做的......

  1. 录制音频和保存剪辑(选中,我这样做了AVAudioRecorder)
  2. 改变音高(选中,使用Dirac做过)
  3. 修剪:(

我有两个标记,即开始和结束偏移,并使用此信息我想修剪记录的文件,并希望保存回来.我不想使用"搜索",因为后来我想要同步播放所有录制的文件(就像时间轴中的flash影片剪辑一样),最后我想导出为一个音频文件.

小智 27

这是我用来修剪预先存在的文件中的音频的代码.如果您已保存或正在保存为其他格式,则需要更改与M4A相关的常量.

- (BOOL)trimAudio
{
    float vocalStartMarker = <starting time>;
    float vocalEndMarker = <ending time>;

    NSURL *audioFileInput = <your pre-existing file>;
    NSURL *audioFileOutput = <the file you want to create>;

    if (!audioFileInput || !audioFileOutput)
    {
        return NO;
    }

    [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
    AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                            presetName:AVAssetExportPresetAppleM4A];

    if (exportSession == nil)
    {        
        return NO;
    }

    CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 100)), 100);
    CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    exportSession.outputURL = audioFileOutput;
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.timeRange = exportTimeRange;

    [exportSession exportAsynchronouslyWithCompletionHandler:^
     {
         if (AVAssetExportSessionStatusCompleted == exportSession.status)
         {
             // It worked!
         } 
         else if (AVAssetExportSessionStatusFailed == exportSession.status)
         {
             // It failed...
         }
     }];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

还有技术问答1730,它提供了更详细的方法.