如何正确处理audio中断?

我已经创build了一个OpenGL 3D游戏,利用OpenAL进行audio播放,如果在audio设备初始化之前按下“Home”button,则会遇到audio丢失的问题。 我试图挂接到audio会话中断处理程序,但我的callback永远不会被调用。 无论我是否最小化或最大化我的申请。 我的“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); } } 

请注意,目前有audio中断和IOS问题。 中断通知是好的,但结束audio中断通知不总是工作。 苹果在这方面有一个bug,他们没有回应。

尝试在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() ); } }