4

I have a problem I am not able to solve. My application should be able to switch the default audio device during runtime. To achieve this I am using XAudio2 from the DirectXTK.

I implemented the IMMNotificationClient into my audio class to be able to react on default device changes.

For instance when the default device changes I stop my current source voice, reset the audio engine and start the source voice again. Everything works as expected.

However when my default device is a USB soundcard and I unplug it during the source voice is playing the application freezes.

The reason for this is that the source voice hangs when stopping the voice. Sometimes also when flushing the source buffer. Seems like the source voice could not be stopped anymore when the audio device was removed the source voice was using.

Does someone had the same problem and was able to solve this?

Here's the function I am using to reset the audio engine.

HRESULT DX::XAudioEngine::ResetAudioDevice()
{
    HRESULT hr = S_OK;

    this->m_retryAudio = TRUE;

    if (SUCCEEDED(hr) && m_pSourceVoice)
    {
        hr = m_pSourceVoice->Stop();
    }

    if (SUCCEEDED(hr))
    {
        hr = m_pSourceVoice->FlushSourceBuffers();
    }

    if (m_audEngine && m_pSourceVoice)
    {
        // Get the source voice back from the smart pointer
        IXAudio2SourceVoice* ptmpSrcVoice = nullptr;
        ptmpSrcVoice = m_pSourceVoice.release();

        // Destroy the voice
        ptmpSrcVoice->DestroyVoice();
    }

    m_audEngine->Reset(&m_waveFormat, NULL);

    // Create source voice
    IXAudio2SourceVoice* ptmpSrcVoice = nullptr;

    m_audEngine->AllocateVoice(
        (WAVEFORMATEX*)&m_waveFormat,
        SoundEffectInstance_Default,
        false,
        &ptmpSrcVoice
    );

    // Add source voice to smart pointer
    m_pSourceVoice.reset(ptmpSrcVoice);

    // Set the input volume
    if (this->m_inputVolume != 1.0f) {
        hr = m_pSourceVoice->SetVolume(this->m_inputVolume);
    }

    hr = m_pSourceVoice->Start(0);

    this->m_retryAudio = FALSE;

    return hr;
}
datoml
  • 5,554
  • 4
  • 21
  • 28
  • What version of OS and XAudio are you using? – Chuck Walbourn Feb 01 '18 at 18:47
  • I am on Windows 10 and using the XAudio 2.7 version. The one from the legacy directx sdk cause we wanted to support windows 7. – datoml Feb 01 '18 at 18:53
  • Have you tried to repro the problem on Windows 7 SP1? You should also try rebuilding with XAudio 2.8/2.9 on Windows 10 and see if you have a repro there. – Chuck Walbourn Feb 01 '18 at 18:54
  • Unfortunately no. But we target now XAudio 2.8 and above and there XAudio2 is able to switch under the hood. – datoml Feb 01 '18 at 19:21
  • @ChuckWalbourn I am currently facing the same issue in a XAudio 2.7 game. Unfortunately, upgrading to XAudio 2.9 is not an option as we're using XACTEngine. I looked left and right and tried many workarounds, but no solution so far. This gamedev.net post has a good reproduction code of the issue: https://www.gamedev.net/forums/topic/706663-xaudio2_7-dont-raise-oncriticalerror-when-headphones-unplugged-and-app-may-hangs/ – Tom Solacroup Jun 08 '23 at 16:19

0 Answers0