XAudio2 create() failed

2.2k views Asked by At

When I try to use XAudio2 it already fails at the XAudio2Create(...) function. (INITIALIZE returns at this line: if (FAILED(hr)) return false; I don't know why and google only tells me to install the directx redistributable, which I did but nothing changed... Any Ideas?

This is basicly code from MSDN:

IXAudio2* pXAudio2 = NULL;

bool INITIALIZE()
{
    HRESULT hr = XAudio2Create(&pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR);
    if (FAILED(hr)) return false;
    .
    .
    .
}

I tried this but none of those Error-Codes seem to match:

HRESULT hr = XAudio2Create(&pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR);
std::cout << "hresult: " << hr << std::endl;
if (hr == XAUDIO2_E_DEVICE_INVALIDATED) std::cout << "XAUDIO2_E_DEVICE_INVALIDATED";
if (hr == XAUDIO2_E_INVALID_CALL) std::cout << "XAUDIO2_E_INVALID_CALL";
if (hr == XAUDIO2_E_XAPO_CREATION_FAILED) std::cout << "XAUDIO2_E_XAPO_CREATION_FAILED";
if (hr == XAUDIO2_E_XMA_DECODER_ERROR) std::cout << "XAUDIO2_E_XMA_DECODER_ERROR";
if (hr == S_OK) std::cout << "S_OK";

Output:

hresult: -2147221008
2

There are 2 answers

1
Lightness Races in Orbit On

Never mind Google: use the documentation:

Returns S_OK if successful, an error code otherwise. See XAudio2 Error Codes for descriptions of XAudio2 specific error codes.

Simply check that error code to find out what's going on.

0
theCNG27 On

Ok got it... I forgot the CoInitializeEx(NULL, COINIT_MULTITHREADED); before the code. Now it returns S_OK!