iOS的音频修剪 [英] iOS Audio Trimming
问题描述
我搜索了很多,但没有找到任何相关信息......我的工作在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...
- 录制音频和保存剪辑(经过,我这样做是使用
AVAudioRecorder
) - 改变音高(查了一下,这篇用狄拉克)
- 修整:(
我有两个标志,即开始与放大器;结束偏移量和使用这种信息,我想剪裁记录的文件,并希望把它救回来。我不想用闹,因为后来我要玩的所有记录文件同步(就像在时间轴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.
推荐答案
下面是我用从pre-现有文件修剪音频code。你需要改变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;
}
有也技术Q&安培; A 1730 ,这给出了一个略为详细的方法。
There's also Technical Q&A 1730, which gives a slightly more detailed approach.
这篇关于iOS的音频修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!