If you want to play the recording, then yes, you must stop recording before you can load the file into an AVAudioPlayer instance.
If you want to play a recording, then add more to the recording after listening to it or say the recording in the middle. Then you have some problems.
You need to create a new audio file and then merge them.
This was my solution:
// Generate a composition of the two audio assets that will be combined into // a single track AVMutableComposition* composition = [AVMutableComposition composition]; AVMutableCompositionTrack* audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; // grab the two audio assets as AVURLAssets according to the file paths AVURLAsset* masterAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:self.masterFile] options:nil]; AVURLAsset* activeAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:self.newRecording] options:nil]; NSError* error = nil; // grab the portion of interest from the master asset [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, masterAsset.duration) ofTrack:[[masterAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error]; if (error) { // report the error return; } // append the entirety of the active recording [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, activeAsset.duration) ofTrack:[[activeAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:masterAsset.duration error:&error]; if (error) { // report the error return; } // now export the two files // create the export session // no need for a retain here, the session will be retained by the // completion handler since it is referenced there AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetAppleM4A]; if (nil == exportSession) { // report the error return; } NSString* combined = @"combined file path";// create a new file for the combined file // configure export session output with all our parameters exportSession.outputURL = [NSURL fileURLWithPath:combined]; // output path exportSession.outputFileType = AVFileTypeAppleM4A; // output file type [exportSession exportAsynchronouslyWithCompletionHandler:^{ // export status changed, check to see if it done, errored, waiting, etc switch (exportSession.status) { case AVAssetExportSessionStatusFailed: break; case AVAssetExportSessionStatusCompleted: break; case AVAssetExportSessionStatusWaiting: break; default: break; } NSError* error = nil; // your code for dealing with the now combined file }];
I cannot take full responsibility for this work, but it was assembled along with the introduction of several others:
AVAudioRecorder / AVAudioPlayer - add an entry to the file
(I can not find another link at the moment)
source share