AVAudioRecorder / AVAudioPlayer - add recording to a file

Is there a way to write to the end of an audio file? We cannot just pause the recording, and not stop it, because the user must return to the application again and add more sound to his recording. Sound is currently stored in CoreData as NSData. NSData AppendData does not work because the received audio file still reports that it is as long as the original data.

Another possibility is to take the original audio file with the new one and combine them into one audio file, if there is a way to do this.

+7
source share
3 answers

This can be done quite easily using AVMutableComposionTrack insertTimeRange:ofTrack:atTime:error . The code is somewhat long, but it is really the same as 4 steps:

 // Create a new audio track we can append to AVMutableComposition* composition = [AVMutableComposition composition]; AVMutableCompositionTrack* appendedAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; // Grab the two audio tracks that need to be appended AVURLAsset* originalAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:originalAudioPath] options:nil]; AVURLAsset* newAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:newAudioPath] options:nil]; NSError* error = nil; // Grab the first audio track and insert it into our appendedAudioTrack AVAssetTrack *originalTrack = [originalAsset tracksWithMediaType:AVMediaTypeAudio]; CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, originalAsset.duration); [appendedAudioTrack insertTimeRange:timeRange ofTrack:[originalTrack objectAtIndex:0] atTime:kCMTimeZero error:&error]; if (error) { // do something return; } // Grab the second audio track and insert it at the end of the first one AVAssetTrack *newTrack = [newAsset tracksWithMediaType:AVMediaTypeAudio]; timeRange = CMTimeRangeMake(kCMTimeZero, newAsset.duration); [appendedAudioTrack insertTimeRange:timeRange ofTrack:[newTrack objectAtIndex:0] atTime:originalAsset.duration error:&error]; if (error) { // do something return; } // Create a new audio file using the appendedAudioTrack AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetAppleM4A]; if (!exportSession) { // do something return; } NSString* appendedAudioPath= @""; // make sure to fill this value in exportSession.outputURL = [NSURL fileURLWithPath:appendedAudioPath]; exportSession.outputFileType = AVFileTypeAppleM4A; [exportSession exportAsynchronouslyWithCompletionHandler:^{ // exported successfully? switch (exportSession.status) { case AVAssetExportSessionStatusFailed: break; case AVAssetExportSessionStatusCompleted: // you should now have the appended audio file break; case AVAssetExportSessionStatusWaiting: break; default: break; } NSError* error = nil; }]; 
+6
source

You can add two audio files by creating an AVMutableCompositionTrack after adding two files and exporting the composition using the exportAsynchronouslyWithCompletionHandler AVAssetExportSession method.

See the links below for more information.

Link to class AVAssetExportSession

Creating New Assets

Hope this helps solve your problem.

+4
source

I do not have a complete code example, but advanced audio file services can help you merge two audio files. Find advanced audio file services in Xcode or follow the link below.

Apple Documentation

+1
source

All Articles