Windows Dev Center

How to: Initialize XAudio2

XAudio2 is initialized for audio playback by creating an instance of the XAudio2 engine, and creating a mastering voice.

Ee415779.wedge(en-us,VS.85).gifTo initialize XAudio2

  1. 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;
    
    
    
  2. 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;


Note  Ensure that all smart pointers to XAUDIO2 objects are fully released before you release the IXAudio2 object.

Related topics

XAudio2 Getting Started
How to: Load Audio Data Files in XAudio2
How to: Play a Sound with XAudio2

 

 

Community Additions

ADD
Show:
© 2015 Microsoft