如何修剪用EZAudioRecorder录制的音频?

May*_*ain 5 objective-c avfoundation avaudiorecorder ios ezaudio

我想修剪用EZAudioRecorder录制的音频.

我正在编写此代码来修剪音频.这对于使用AVAudioRecorder录制的音频工作正常但是它会触发EZAudioRecorder的错误阻止,错误无法打开文件.

-(BOOL)trimAudiofile{
   float audioStartTime=1.0;
   float audioEndTime=2.0;//define end time of audio
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
   NSString *libraryCachesDirectory = [paths objectAtIndex:0];
   libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];
   NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/output_%@.m4a", [dateFormatter stringFromDate:[NSDate date]]];
   NSURL *audioFileOutput = [NSURL fileURLWithPath:OutputFilePath];
   NSURL *audioFileInput=[self testFilePathURL];//<Path of orignal audio file>

   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(audioStartTime * 100)), 100);
 CMTime stopTime = CMTimeMake((int)(ceil(audioEndTime * 100)), 100);
 CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

 exportSession.outputURL = audioFileOutput;
 exportSession.timeRange = exportTimeRange;
 exportSession.outputFileType = AVFileTypeAppleM4A;

[exportSession exportAsynchronouslyWithCompletionHandler:^
{
   if (AVAssetExportSessionStatusCompleted == exportSession.status)
   {
     NSLog(@"Export OK");
   }
   else if (AVAssetExportSessionStatusFailed == exportSession.status)
   {
     NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
   }
 }];
 return YES;
}
Run Code Online (Sandbox Code Playgroud)

注意: - 音频文件存在于文档目录中,EZAudioPlayer也可以播放此文件.

谁能告诉我我哪里做错了?任何有关这方面的帮助将不胜感激.

提前致谢.

May*_*ain 0

谢谢大家..我找到了这个问题的解决方案...!!

我没有关闭音频文件。我在修剪音频之前添加了这段代码,现在一切都按预期工作。

即使文件存在于文档目录中,我们也需要关闭该文件。

if (self.recorder)
{
    [self.recorder closeAudioFile];
}
Run Code Online (Sandbox Code Playgroud)

看看这个 git 问题