kAudioSessionIncompatibleCategory

A Note About kAudioSessionIncompatibleCategory Error on iOS 5 and 6

There is bug apparent on both iOS 5 and 6 (but not iOS 7) that manifests in apps that use the PlayAndRecord audio session category under certain multitasking conditions. The issue is triggered reliably via the following steps:

Open one app that uses both PlayAndRecord and background audio.
Open a second app that also uses PlayAndRecord and background audio.
Quit the first app, via the multitasking bar.
Open the first app again. The app will experience the error.
The error can also be caused by launching certain combinations of apps.

It results in a completely inoperable audio session, as well as a kAudioSessionIncompatibleCategory ('!cat') error when querying the kAudioSessionProperty_CurrentHardwareInputNumberChannels audio session property.

Once the error appears, the problem can only be resolved by quitting some or all running audio apps - unless the workaround described below is used.

See rdar://13022588 for further description.

We strongly recommend writing code to recognize this error state, and responds appropriately: First by implementing the workaround described below, and in the event of a failure in the workaround, informing the user accordingly, recommending that the user relaunches their apps.

Here's our proposed message to the user:

You’ve discovered a known issue with the iOS audio system that we have no control over.

Please relaunch all your music apps, making sure you launch the Audiobus app first.

You can frequently recover from this error by deactivating and reactivating the audio session:

AudioSessionSetActive(false);
AudioSessionSetActive(true);
This seems to resolve the issue when it arises sometimes, but at other times, it has no effect.

Nevertheless, we recommend making use of it to mitigate some of the effects of the bug, until Apple fix it.

The implementation of the workaround is simple: Early in your app's audio initialization, check the kAudioSessionProperty_CurrentHardwareInputNumberChannels property. If you get a kAudioSessionIncompatibleCategory error, restart the audio session:

UInt32 channels;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareInputNumberChannels, &size, &channels);
if ( result == kAudioSessionIncompatibleCategory ) {
   // Audio session error (rdar://13022588). Power-cycle audio session.
   AudioSessionSetActive(false);
   AudioSessionSetActive(true);
   result = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareInputNumberChannels, &size, &channels);
   if ( result != noErr ) {
       NSLog(@"Got error %d while querying input channels", result);
   }
}


If there is no need for U to know the kAudioSessionProperty_CurrentHardwareInputNumberChannels value, you can delete the AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareInputNumberChannels, &size, &channels); to avoid the problem.

你可能感兴趣的:(session)