How can I trim the sound recorded using EZAudioRecorder?

I want to trim audio recorded using EZAudioRecorder .

I am writing this code to trim audio. This works great for sound recorded using AVAudioRecorder , but it causes an error block with EZAudioRecorder , and the file could not be opened when an error occurred .

-(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 : - An audio file exists in the document directory, and EZAudioPlayer can also play this file.

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

Thanks in advance.

+5
source share
2 answers

Thanks to everyone .. I found a solution to this problem ... !!

I did not close the audio file. I added this code before trimming the sound, now everything works as expected.

We need to close this file even with the files in the document directory.

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

look at this git problem

0
source

1. Check file formats in both cases when you trim the sound recorded by AVAudioRecorder and sound using EZAudioRecorder .

2. You are trying to successfully export the file before recording sound, you need to wait until the sound is recorded.

0
source

All Articles