Question

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

Was it helpful?

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.

OTHER TIPS

Here is the document about how to hand AudioSession interruption.

OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, NULL);

#pragma mark AudioSession listeners
void interruptionListener(  void *  inClientData,
                          UInt32    inInterruptionState)
{
    if (inInterruptionState == kAudioSessionBeginInterruption)
    {
          //handle begin interruption
    }
    else if(inInterruptionState == kAudioSessionEndInterruption){
          //handle end  interruption
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top