Objective - C,我如何裁剪 .caf 文件?
Posted
技术标签:
【中文标题】Objective - C,我如何裁剪 .caf 文件?【英文标题】:Objective - C, how do I crop .caf file? 【发布时间】:2013-03-01 10:03:49 【问题描述】:我已使用 AVAudioRecorder
将声音录制到 .caf 文件中。
我想剪切/修剪这个文件的前 2 秒。怎么做? (我花了好几个小时才找到解决方案,但是不走运)
【问题讨论】:
【参考方案1】:您可以使用 AVAssetExportSession.. 来做到这一点。
试试这个代码..
float vocalStartMarker = 0.0f;
float vocalEndMarker = 2.0f;
NSURL *audioFileInput = @"input filePath"; // give your audio file path
NSString *docsDirs;
NSArray *dirPath;
dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDirs = [dirPath objectAtIndex:0];
NSString *destinationURLs = [docsDirs stringByAppendingPathComponent:@"trim.caf"];
audioFileOutput = [NSURL fileURLWithPath:destinationURLs];
if (!audioFileInput || !audioFileOutput)
return NO;
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVURLAsset URLAssetWithURL:audioFileInput options:nil];
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!
NSLog(@"DONE TRIMMING.....");
NSLog(@"ouput audio trim file %@",audioFileOutput);
else if (AVAssetExportSessionStatusFailed == exportSession.status)
// It failed...
NSLog(@"FAILED TRIMMING.....");
];
【讨论】:
以上是关于Objective - C,我如何裁剪 .caf 文件?的主要内容,如果未能解决你的问题,请参考以下文章
Objective-C:从 UIScrollView 获取缩放的裁剪图像