user1873452
user1873452

Reputation: 75

Resume recording after the AVAudioRecorder session is released

I am using the AVAudioRecorder class for recording the sounds. It is possible to start, pause, resume, stop the recording sounds in one AVAudioRecorder session. If the app closes and starts again, it is not possible to resume the recording to already exiisting audio files.

Issues with recording and playing .wav files IOS

Please refer to my question (previously asked here) to have a look at my code to initialize audio recorder.

Is it possible to do so? Please help! Thanks!

Upvotes: 1

Views: 777

Answers (1)

Pradip
Pradip

Reputation: 707

I think you have to save audio recording files separately to your document directory each time your app closes and after start of the app mix the all audio recording as available.

Here the code for mixing your audio files ,In your case you just need to mixed two files hence need to use code accordingly.

-(void)mixAudios
{
//mix all audio files
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio  preferredTrackID:kCMPersistentTrackID_Invalid];

for (number of your recordings)
{

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"** Recording file Path **"] options:nil];

    if(![compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(Start time in seconds for each Recoring,1) error:nil])
    {
         NSLog(@" error: %@"error);
    }

}


//save mixed composition as file
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                      presetName:AVAssetExportPresetPassthrough];

NSString* videoName = @".mov";
NSString* videoPath = [[NSString alloc] initWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], videoName];
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
if([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void )
 {
     [_assetExport release];
     NSString *path = [NSString stringWithString:[exportUrl path]];
     if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (path)) {
         UISaveVideoAtPathToSavedPhotosAlbum (path, nil, nil, nil);
     }

 }

 ];
}

Upvotes: 1

Related Questions