我有这个问题。我正在通过 AVAssetExportSession 修剪声音文件。我设置时间范围然后异步导出。我以与输入文件不同的名称保存输出文件。
它工作正常,但只是第一次。当我尝试修剪修剪后的文件时,它会以整个持续时间导出它,但 CMTimeRangeShow 显示正确的时间范围。
有人知道我做错了什么吗?
最佳答案
我不确定我的代码现在是否对您可用,因为它适用于 iOS7。希望对您有所帮助。
- (BOOL)trimAudio :(NSURL *) url
{
float vocalStartMarker = timeFrom;
float vocalEndMarker = timeTo;
NSURL *audioFileInput = url_Audio;
NSURL *audioFileOutput = url;
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...
[[[UIAlertView alloc]initWithTitle:@"Unknown Error" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]show];
}
}];
return YES;
}
关于ios - AVAssetExportSession 不导出时间范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20203548/