Voice recording in the background using AVAudioRecorder

I am trying to record a user's voice in the background using AVAudioRecorder , but every time I get NO when calling the recordForDuration method. I add "audio" to UIBackgroundModes , but that does not help. By the way, everything works perfectly in the foreground. I initialize AVAudioRecorder with this code:

 NSError* error; if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error]) { NSLog(@"Error while setting audio session category. Code - %d, description - \"%@\".", [error code], [error localizedDescription]); return; } NSURL* outputFileURL = [VKMFileSystemHelper uniqueFileInPath:[[VKMFileSystemHelper subdirectoryInsideLibrary:DIR_AUDIO] path] withExtension:@"m4a"]; NSDictionary* recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithInt:AVAudioQualityMin] , AVEncoderAudioQualityKey, [NSNumber numberWithInt:16] , AVEncoderBitRateKey, [NSNumber numberWithInt:1] , AVNumberOfChannelsKey, [NSNumber numberWithFloat:12000.0] , AVSampleRateKey, nil]; _recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSettings error:&error]; [_recorder setDelegate:self]; if (error) { NSLog(@"Error occured during audio recorder initialization. Error code - %d, description - \"%@\".", [error code], [error localizedDescription]); } else { NSLog(@"Start recording."); if ([_recorder recordForDuration:[shedule execLength]]) { NSLog(@"Record started."); } else { NSLog(@"Record failed."); } } 

How can I record a microphone from the background? Is it possible?

+1
ios background avaudiorecorder
Mar 07 '13 at 6:28
source share
1 answer

Try it,

  ***Appdelegate.m*** - (void)applicationDidEnterBackground:(UIApplication *)application { __block UIBackgroundTaskIdentifier task = 0; task=[application beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"Expiration handler called %f",[application backgroundTimeRemaining]); [application endBackgroundTask:task]; task=UIBackgroundTaskInvalid; }]; } 
+3
Apr 30 '13 at 8:14
source share



All Articles