iOS AudioSession vs mixer

Thanks for your patience.

I’ve done some more testing, but presumably due to iOS differences and given that iOS 13 is no longer supported by Apple, I cannot reproduce the issue. What versions have your users been able to reproduce it on? Have they run into it on versions above iOS 15? If so, which?

With that said, I can see how the particular combination of receiving a “interruption began” after a session has already become active, but without a corresponding “interruption end”, could cause problems. For this case, all I can recommend is to set the AudioSession as active and resume the mixer when the user performs an input. As a simple demonstration, I’ve modified common_platform.mm in our iOS examples as follows:

  • Created a boolean appIsActive to track whether the app is active or not:
bool gIsSuspended = false;
bool gNeedsReset = false;
bool isAppActive = false;     // new code here
  • Set appIsActive to true at the end of Common_Init:
/*
    Activate the audio session
*/
success = [session setActive:TRUE error:nil];
assert(success);
appIsActive = true;    // new code here
  • Set appIsActive to true in the existing UIApplicationDidBecomeActive observer:
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *notification)
{
    NSLog(@"Application did become active");
    appIsActive = true;    // new code here
// ...
}
  • Set appIsActive to false in the existing UIApplicationWillResignActive observer:
[[NSNotificationDenter defaultCenter] addObserverForName:UIAppliCationWillResignActiveNotification object:nil queue:nil usingBlock:^(NSNotification *notification)
{
    NSLog(@"Application will resign active");
    appIsActive = false;    // new code here
}
  • At the end of update, if the app is active and the user performs inputs, try to activate the AudioSession and resume the mixer:
void (update)
{
    
    // ...
    gButtonsPress = ((gButtonsDown ^ gButtons) & gButtons) | gActions;
    gButtonsDown = gButtons;
    gActions = 0;

    // new code start
    if (gButtons != 0 && appIsActive)
    {    if ([[AVAudioSession sharedInstance] setActive:TRUE error:nil])
        {
            gIsSuspended = false;
            gSuspendCallback(false);
        }
    }
    // new code end
}

This should resolve the issue, but I can’t be sure since I’m not able to reproduce the issue on my end. However, I’ve also added this particular case, and the advice to check for inputs to activate the AudioSession, to the improvements we’re making to the iOS platform docs and examples that I mentioned previously.