iOSaudio修剪

我search了很多,找不到任何相关的…我正在研究iOSaudio文件,这是我想要做的…

  1. 录制audio和保存剪辑(检查,我用AVAudioRecorder这样AVAudioRecorder
  2. 改变音调(检查,这是否使用Dirac)
  3. 修剪:(

我有两个标记,即开始和结束偏移量,并使用此信息我想修剪录制的文件,并希望将其保存回来。 我不想使用“寻找”,因为后来我想同步播放所有录制的文件(就像时间轴上的Flash影片剪辑一样),最后我想导出为一个audio文件。

以下是我用来从预先存在的文件中修剪audio的代码。 如果您已经保存或正在保存为其他格式,则需要更改与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; } 

还有技术问答1730 ,它提供了一个稍微更详细的方法。

这里是一个示例代码,从开始和结束偏移修剪audio文件,并将其保存回来。 请检查这个iOSaudio剪辑 。

 // Path of your source audio file NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"abc.mp3"]; NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath]; // Path of your destination save audio file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryCachesDirectory = [paths objectAtIndex:0]; libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"]; NSString *strOutputFilePath = [NSString stringWithFormat:@"%@%@",libraryCachesDirectory,@"/abc.mp4"]; NSURL *audioFileOutput = [NSURL fileURLWithPath:strOutputFilePath]; 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; } float startTrimTime = 0; float endTrimTime = 5; CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100); CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); exportSession.outputURL = audioFileOutput; exportSession.outputFileType = AVFileTypeAppleM4A; exportSession.timeRange = exportTimeRange; [exportSession exportAsynchronouslyWithCompletionHandler:^ { if (AVAssetExportSessionStatusCompleted == exportSession.status) { NSLog(@"Success!"); } else if (AVAssetExportSessionStatusFailed == exportSession.status) { NSLog(@"failed"); } }]; 

在.m中导入以下两个库

 #import "BJRangeSliderWithProgress.h" #import < AVFoundation/AVFoundation.h > 

然后粘贴下面的代码,你将能够修剪一个audio文件的帮助下,两个拇指。

 - (void) viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. mySlider = [[BJRangeSliderWithProgress alloc] initWithFrame:CGRectMake(20, 100, 300, 50)]; [mySlider setDisplayMode:BJRSWPAudioSetTrimMode]; [mySlider addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged]; [mySlider setMinValue:0.0]; NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"]; NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath]; audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:audioFileInput error:nil]; [mySlider setMaxValue:audioPlayer.duration]; [self.view addSubview:mySlider]; } -(void)valueChanged { NSLog(@"%f %f", mySlider.leftValue, mySlider.rightValue); } -(IBAction)playTheSong { // Path of your source audio file NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"]; NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath]; // Path of your destination save audio file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryCachesDirectory = [paths objectAtIndex:0]; //libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"]; NSString *strOutputFilePath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.mov"]; NSString *requiredOutputPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"]; NSURL *audioFileOutput = [NSURL fileURLWithPath:requiredOutputPath]; [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL]; AVAsset *asset = [AVAsset assetWithURL:audioFileInput]; AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A]; float startTrimTime = mySlider.leftValue; float endTrimTime = mySlider.rightValue; CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100); CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); exportSession.outputURL = audioFileOutput; exportSession.outputFileType = AVFileTypeAppleM4A; exportSession.timeRange = exportTimeRange; [exportSession exportAsynchronouslyWithCompletionHandler:^ { if (AVAssetExportSessionStatusCompleted == exportSession.status) { NSLog(@"Success!"); NSLog(@" OUtput path is \n %@", requiredOutputPath); NSFileManager * fm = [[NSFileManager alloc] init]; [fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil]; //[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL]; NSURL *url=[NSURL fileURLWithPath:requiredOutputPath]; NSError *error; audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops=0; [audioPlayer play]; } else if (AVAssetExportSessionStatusFailed == exportSession.status) { NSLog(@"failed with error: %@", exportSession.error.localizedDescription); } }]; }