Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly handle audio interruptions?

I've created a OpenGL 3D game utilizing OpenAL for audio playback and experienceing a problem of losing audio if "Home" button is getting pressed before audio device is getting initialized. I tried to hook up to audio session interrupt handler, but my callback is never getting called. No matter if I minimize or maximize my application. My "OpenALInterruptionListener" is never getting called.

What am I doing wrong?

AudioSessionInitialize(NULL, NULL, OpenALInterriptionListener, this);

void OpenALInterriptionListener(void * inClientData, UInt32 inInterruptionState)
{
    OpenALDevice * device = (OpenALDevice *) inClientData;

    if (inInterruptionState == kAudioSessionBeginInterruption)
    {
          alcSuspendContext(_context);
          alcMakeContextCurrent(_context);
          AudioSessionSetActive(false);
    }
    else if (inInterruptionState == kAudioSessionEndInterruption)
    {
          UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
          AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
          AudioSessionSetActive(true);    
          alcMakeContextCurrent(_context);
          alcProcessContext(_context);
    }
}
like image 897
rubenhak Avatar asked May 16 '12 18:05

rubenhak


People also ask

What is audio focus in Android?

Common Audio Focus use cases Many apps on your Android phone can play audio simultaneously. While the Android operating system mixes all audio streams together, it can be very disruptive to a user when multiple apps are playing audio at the same time. This results in the user having a poor experience on their phone.


1 Answers

Please note that there are currently issues with Audio Interruptions and IOS. Interruption notifications are fine, but end Audio Interruptions Notifications do not always work. There is a bug into Apple on this and they have not responded.

like image 85
ort11 Avatar answered Sep 28 '22 22:09

ort11