6

I want to trim an audio which is recorded with EZAudioRecorder.

I am writing this code to trim an audio. This is working fine for audio recorded with AVAudioRecorder but it triggers error block with EZAudioRecorder, with an error couldn't open file.

-(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;
}

Note:- The audio file exists in document directory and EZAudioPlayer is also able to play this file.

Can anyone tell me where am I doing wrong ? Any help on this will be appreciated.

Thanks in advance.

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65

2 Answers2

0

1.Check file formate in both cases when you trim audio recorded by AVAudioRecorder and audio by EZAudioRecorder.

2.You are trying to export file before record audio successfully,you have to wait until audio is not recorded.

Pankaj purohit
  • 485
  • 3
  • 10
  • can you please describe with code where am I doing wrong. And before trim we have to export the file after that we can perform trim operation. and this code is running perfect with an audio recorded with AVAudioRecorder. – Mayank Jain Jul 17 '15 at 12:30
  • I have an audio file which is previously recorded with EZAudioRecorder. and for performing trim operation I am exporting this audio file and giving a new path for saving trimmed audio. everything is going great if I record audio with AVAudioRecorder but not with EZAudioRecorder. – Mayank Jain Jul 17 '15 at 12:35
0

Thanks all.. I found the solution of this issue...!!

I was not closing audio file. I added this code before trimming audio, Now everything working as expecting.

We need to close this file even file exits in document directory.

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

have a look on this git issue

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65