我想实现一个功能,让用户修剪他以前记录的音频文件(.caf).录音部分已经可以使用,但是我如何添加类似于Voicememos应用程序中的修剪功能.苹果使用的音频修剪器是否有api?
任何帮助都会很棒……
任何帮助都会很棒……
解决方法
使用AVFoundation怎么样?将音频文件导入AVAsset(合成等),然后您可以将其 – 设置首选持续时间 – 导出到文件.
我之前编写了一个将资产导出到文件的股票函数,你也可以指定一个audiomix.如下所示,它导出所有文件,但你可以将一个NSTimeRange添加到exporter.timeRange,然后你去.我没有测试过,但应该工作(?).另一种方法是在创建AVAsset轨道时调整时间范围.当然,出口商只处理m4a(AAC).对不起,如果这不是你想要的.
-(void)exportAsset:(AVAsset*)asset toFile:(NSString*)filename overwrite:(BOOL)overwrite withMix:(AVAudioMix*)mix { //NSArray* availablePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]; AVAssetExportSession* exporter = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A]; if (exporter == nil) { DLog(@"Failed creating exporter!"); return; } DLog(@"Created exporter! %@",exporter); // Set output file type DLog(@"Supported file types: %@",exporter.supportedFileTypes); for (NSString* filetype in exporter.supportedFileTypes) { if ([filetype isEqualToString:AVFileTypeAppleM4A]) { exporter.outputFileType = AVFileTypeAppleM4A; break; } } if (exporter.outputFileType == nil) { DLog(@"Needed output file type not found? (%@)",AVFileTypeAppleM4A); return; } // Set outputURL NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString* parentDir = [NSString stringWithFormat:@"%@/",[paths objectAtIndex:0]]; NSString* outPath = [NSString stringWithFormat:@"%@%@",parentDir,filename]; NSFileManager* manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:outPath]) { DLog(@"%@ already exists!",outPath); if (!overwrite) { DLog(@"Not overwriting,uh oh!"); return; } else { // Overwrite DLog(@"Overwrite! (delete first)"); NSError* error = nil; if (![manager removeItemAtPath:outPath error:&error]) { DLog(@"Failed removing %@,error: %@",outPath,error.description); return; } else { DLog(@"Removed %@",outPath); } } } NSURL* const outUrl = [NSURL fileURLWithPath:outPath]; exporter.outputURL = outUrl; // Specify a time range in case only part of file should be exported //exporter.timeRange = if (mix != nil) exporter.audioMix = mix; // important DLog(@"Starting export! (%@)",exporter.outputURL); [exporter exportAsynchronouslyWithCompletionHandler:^(void) { // Export ended for some reason. Check in status NSString* message; switch (exporter.status) { case AVAssetExportSessionStatusFailed: message = [NSString stringWithFormat:@"Export Failed. Error: %@",exporter.error.description]; DLog(@"%@",message); [self showAlert:message]; break; case AVAssetExportSessionStatusCompleted: { /*if (playfileWhenExportFinished) { DLog(@"playfileWhenExportFinished!"); [self playfileAfterExport:exporter.outputURL]; playfileWhenExportFinished = NO; }*/ message = [NSString stringWithFormat:@"Export completed: %@",filename]; DLog(@"%@",message); [self showAlert:message]; break; } case AVAssetExportSessionStatusCancelled: message = [NSString stringWithFormat:@"Export cancelled!"]; DLog(@"%@",message); [self showAlert:message]; break; default: DLog(@"Export unhandled status: %d",exporter.status); break; } }]; }