How to: Initialize XAudio2
XAudio2 is initialized for audio playback by creating an instance of the XAudio2 engine, and creating a mastering voice.
To initialize XAudio2
-
Use the XAudio2Create function to create an instance of the XAudio2 engine.
IXAudio2* pXAudio2 = NULL; HRESULT hr; if ( FAILED(hr = XAudio2Create( &pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) ) return hr; -
Use the CreateMasteringVoice method to create a mastering voice.
The mastering voices encapsulates an audio device. It is the ultimate destination for all audio that passes through an audio graph.
IXAudio2MasteringVoice* pMasterVoice = NULL; if ( FAILED(hr = pXAudio2->CreateMasteringVoice( &pMasterVoice ) ) ) return hr;
Notes for Windows Store apps
We recommend that you make use of a smart pointer to manage the lifetime of XAUDIO2 objects in an exception safe manner. For Windows Store apps, you can use the ComPtr smart pointer template from the Windows Runtime C++ Template Library (WRL).
Microsoft::WRL::ComPtr<IXAudio2> XAudio2; HRESULT hr; if ( FAILED(hr = XAudio2Create( &XAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) ) throw Platform::Exception::CreateException(hr); IXAudio2MasteringVoice* pMasterVoice = NULL; if ( FAILED(hr = pXAudio2->CreateMasteringVoice( &pMasterVoice ) ) ) return hr;
Related topics