如何正确处理音频中断?

rub*_*hak 12 c++ audio interrupt openal ios

我创建了一个利用OpenAL进行音频播放的OpenGL 3D游戏,如果在音频设备初始化之前按下"Home"按钮,则会遇到丢失音频的问题.我试图连接到音频会话中断处理程序,但我的回调永远不会被调用.无论我是最小化还是最大化我的申请.永远不会调用我的"OpenALInterruptionListener".

我究竟做错了什么?

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

jam*_*345 1

尝试在 alcMakeContextCurrent() 中使用 NULL

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() );
    }
}
Run Code Online (Sandbox Code Playgroud)