iOS 音频修剪 [英] iOS Audio Trimming

查看:16
本文介绍了iOS 音频修剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I searched a lot and couldn't find anything relevant... I am working on iOS audio files and here is what I want to do...

  1. 录制音频并保存剪辑(已选中,我使用 AVAudioRecorder 完成此操作)
  2. 改变音高(已检查,使用狄拉克做到了这一点)
  3. 修剪:(

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

I have two markers i.e. starting & ending offset and using this info I want to trim recorded file and want to save it back. I don't want to use "seek" because later on I want to play all recorded files in sync (just like flash movie clips in timeline) and then finally I want to export as one audio file.

推荐答案

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

Here's the code that I've used to trim audio from a pre-existing file. You'll need to change the M4A related constants if you've saved or are saving to another format.

- (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;
}

还有 技术问答 1730,其中提供了更详细的信息方法.

There's also Technical Q&A 1730, which gives a slightly more detailed approach.

这篇关于iOS 音频修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆