From 'How to Initialize XAudio2' in Windows Developer
The following code example:
#include <iostream>
#include <xaudio2.h>
HRESULT init_audio(void)
{
IXAudio2* pXAudio2 = NULL;
HRESULT hr;
if (FAILED(hr = XAudio2Create(&pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR)))
return hr;
IXAudio2MasteringVoice* pMasterVoice = NULL;
if (FAILED(hr = pXAudio2->CreateMasteringVoice(&pMasterVoice)))
return hr;
return hr;
}
int main()
{
std::cout << "Hello World!\n";
std::cout << std::hex << init_audio() << std::endl;
}
directly into a new console solution returns after failing CreateMasteringVoice with the error 800401F0
.
Using any other compiler (GNU, LLVM, Clang) fails to successfully parse XAudio2.h.
If you use an Error Lookup Tool, you'd see that 800401F0
translates to CO_E_NOTINITIALIZED
.
As noted in the comments, you have to initialize COM first:
HRESULT hr = CoInitializeEx( nullptr, COINIT_MULTITHREADED );
if (FAILED(hr))
// error
I submitted an edit to note that fact on the Microsoft Docs page.
You should also look at the DirectX Tool Kit for Audio for extensive XAudio2 sample code. There's also samples for XAudio2 on GitHub.
clang/LLVM for Windows v10 works for xaudio2.h
in the most recent Windows 10 SDKs, and the XAudio2Redist NuGet package header. Just remember that you need to manually integrate the XAudio2Redist NuGet include/lib if you aren't using MSBuild. Otherwise, if you are using the xaudio2.h
header in the Windows 10 SDK itself, you need to have _WIN32_WINNT
set for Windows 8 or later.