簡體   English   中英

如何修剪用EZAudioRecorder錄制的音頻?

[英]How can I trim an audio recorded with EZAudioRecorder?

我想修剪用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;
}

注意 : - 音頻文件存在於文檔目錄中, EZAudioPlayer也可以播放此文件。

誰能告訴我我哪里做錯了? 任何有關這方面的幫助將不勝感激。

提前致謝。

1.當您修剪AVAudioRecorder錄制的音頻和EZAudioRecorder錄制的音頻時,請檢查兩種情況下的文件格式

2.您正在嘗試在錄制音頻成功之前導出文件,您必須等到音頻未錄制。

謝謝大家..我找到了這個問題的解決方案...... !!

我沒有關閉音頻文件。 我在修剪音頻之前添加了這段代碼,現在一切正常。

我們需要關閉此文件甚至文件目錄中的文件退出。

if (self.recorder)
{
    [self.recorder closeAudioFile];
}

看看這個git問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM