【问题标题】:How to properly handle audio interruptions?如何正确处理音频中断?
【发布时间】:2023-03-18 05:44:01
【问题描述】:

我创建了一个使用 OpenAL 进行音频播放的 OpenGL 3D 游戏,如果在初始化音频设备之前按下“主页”按钮,则会遇到音频丢失的问题。我试图连接到音频会话中断处理程序,但我的回调永远不会被调用。无论我是最小化还是最大化我的应用程序。我的“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);
    }
}

【问题讨论】:

    标签: c++ ios audio interrupt openal


    【解决方案1】:

    请注意,目前存在音频中断和 IOS 问题。中断通知很好,但结束音频中断通知并不总是有效。苹果对此有一个错误,他们没有回应。

    【讨论】:

      【解决方案2】:

      尝试在 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() );
          }
      }
      
      猜你喜欢
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多