How to properly handle audio interruptions?
Asked Answered
K

2

12

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);
    }
}
Koon answered 16/5, 2012 at 18:51 Comment(0)
K
1

Try using NULL in alcMakeContextCurrent()

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

    if( inInterruptionState == kAudioSessionBeginInterruption )
    {
        alcMakeContextCurrent(NULL);    
    }
    else if( inInterruptionState == kAudioSessionEndInterruption )
    {
        nResult = AudioSessionSetActive(true);

        if( nResult )
        {
            //  "Error setting audio session active"
        }

        alcMakeContextCurrent( device->GetContext() );
    }
}
Kaddish answered 28/5, 2012 at 3:21 Comment(1)
O
2

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.

Obese answered 5/6, 2012 at 19:30 Comment(0)
K
1

Try using NULL in alcMakeContextCurrent()

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

    if( inInterruptionState == kAudioSessionBeginInterruption )
    {
        alcMakeContextCurrent(NULL);    
    }
    else if( inInterruptionState == kAudioSessionEndInterruption )
    {
        nResult = AudioSessionSetActive(true);

        if( nResult )
        {
            //  "Error setting audio session active"
        }

        alcMakeContextCurrent( device->GetContext() );
    }
}
Kaddish answered 28/5, 2012 at 3:21 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.