Windows Dev Center

EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

How to: Play a Sound with XAudio2

This topic describes the minimum steps required to play previously-loaded audio data in XAudio2. After you initialize XAudio2 (see How to: Initialize XAudio2) and load the audio data (see How to: How to: Load Audio Data Files in XAudio2), you can play a sound by creating a source voice and passing audio data to it.

To play a sound

  1. Initialize the XAudio2 engine by following the steps described in How to: Initialize XAudio2.
  2. Populate a WAVEFORMATEX and XAUDIO2_BUFFER structure by following the steps described in How to: Load Audio Data Files in XAudio2.
    Note  Depending on the format of the audio data, you may need to use a larger data structure containing a WAVEFORMATEX structure in place of a WAVEFORMATEX. See the WAVEFORMATEX reference page for more information.
  3. Create a source voice by calling the IXAudio2::CreateSourceVoice method on an instance of the XAudio2 engine. The format of the voice is specified by the values set in a WAVEFORMATEX structure.
    
    IXAudio2SourceVoice* pSourceVoice;
    if( FAILED(hr = pXAudio2->CreateSourceVoice( &pSourceVoice, (WAVEFORMATEX*)&wfx ) ) ) return hr;
    
    
  4. Submit an XAUDIO2_BUFFER to the source voice using the function SubmitSourceBuffer.
    
    if( FAILED(hr = pSourceVoice->SubmitSourceBuffer( &buffer ) ) )
        return hr;
    
    
    Note  The audio sample data to which buffer points is still 'owned' by the app and must remain allocated and accessible until the sound stops playing.
  5. Use the Start function to start the source voice. Since all XAudio2 voices send their output to the mastering voice by default, audio from the source voice automatically makes its way to the audio device selected at initialization. In a more complicated audio graph, the source voice would have to specify the voice to which its output should be sent.
    
    if ( FAILED(hr = pSourceVoice->Start( 0 ) ) )
        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<IXAudio2SourceVoice> SourceVoice;
HRESULT hr;
if( FAILED(hr = pXAudio2->CreateSourceVoice( &SourceVoice, (WAVEFORMATEX*)&wfx ) ) )
    throw Platform::Exception::CreateException(hr); 

if( FAILED(hr = SourceVoice->SubmitSourceBuffer( &buffer ) ) )
    throw Platform::Exception::CreateException(hr); 

if ( FAILED(hr = SourceVoice->Start( 0 ) ) )
    throw Platform::Exception::CreateException(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: Initialize XAudio2
How to: Load Audio Data Files in XAudio2

 

 

Ajouts de la communauté

Afficher:
© 2015 Microsoft