Search code examples
iosios7avaudiorecorderavaudiosession

Audio recording won't work after incoming call


I am trying to record voice from microphone and later play it over speaker. I use this code to accomplish it

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

It works fine, but if incoming call received (when app is active), after call is finished and app is resumed, microphone input not detected anymore.

I tried to handle - (void)applicationDidBecomeActive:(UIApplication *)application and reset AVAudioSession, but it won't work. Might I need to pause AVAudioRecorder when app is inactive, and record when it becomes active?

Am I doing something wrong, or it's iOS bug? I'm running on iOS 7. Thanks


Solution

  • You are correct that you need to pause the AVAudioRecorder once a call is received and then resume it once it is ended. However, you don't do that in applicationDidBecomeActive:, instead you need to implement AVAudioSession's delegates, and more specifically audioRecorderBeginInterruption: and audioRecorderEndInterruption:. Have a look here for an example of how to implement these delegates. I would also look at this SO post which should solve your problem.