iOS: Patch for issue with background audio processing


#1

When adding "audio" to "UIBackgroundModes" in the Info.plist for a iOS app it is possible to do audio processing in the background when exiting the application. A red recording status bar appears the the top of the screen when exiting the app.

The issue I ran into was that I sometimes do not want to be recording in the background, however calling close() iOSAudioIODevice does not remove the red recording bar, it only stops iOSAudioIODevice from processing audio.

I found this question on Stack Overflow regarding this issue under other circumstances:

http://stackoverflow.com/questions/16878991/hide-red-recording-status-bar-in-ios-app-when-not-recording

The accepted answer states

The bar will never disappear as long as the mic is in use, recording or not. it's a security measure to allow the user to know that an app is listening to the microphone, not to show that the phone is recording.

The only way to get it gone is to remove the mic from the input receivers

 

The way to fix this is set kAudioSessionProperty to kAudioSessionCategory_MediaPlayback in iOSAudioIODevice::close() and thereby disabling the microphone.

    void close()
    {
        if (isRunning)
        {
            isRunning = false;
            
            UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;            
            AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (audioCategory), &audioCategory);
            
            AudioSessionRemovePropertyListenerWithUserData (kAudioSessionProperty_AudioRouteChange, routingChangedStatic, this);
            AudioSessionSetActive (false);

            if (audioUnit != 0)
            {
                AudioComponentInstanceDispose (audioUnit);
                audioUnit = 0;
            }
        }
    }


This of course is only necessary when the kAudioSessionProperty has been set to kAudioSessionCategory_PlayAndRecord in open. Also note that this property will always be set back to the proper value when iOSAudioIODevice::open is called again.

I have verified that this patched version of close() correctly removes the red recording status bar on iOS 7.


#2

Ah, good call, thanks! Will sort that out..