title: I cannot play a sound twice in xaudio2
body: <p>I am trying to set up xaudio2 and have been following the documentation. I can play a sound just fine, until I try playing it again, at which point nothing happens.
  I have followed the documentation almost line for line, but can't figure out why this is happening.
  here is the code.</p>
  <pre><code>        #include &lt;iostream&gt;
      #include &lt;vector&gt;
      #include &lt;algorithm&gt;
      #include &lt;xaudio2.h&gt;
      #include &lt;Windows.h&gt;
  
      #ifdef _XBOX //Big-Endian
      #define fourccRIFF 'RIFF'
      #define fourccDATA 'data'
      #define fourccFMT 'fmt '
      #define fourccWAVE 'WAVE'
      #define fourccXWMA 'XWMA'
      #define fourccDPDS 'dpds'
      #endif
  
      #ifndef _XBOX //Little-Endian
      #define fourccRIFF 'FFIR'
      #define fourccDATA 'atad'
      #define fourccFMT ' tmf'
      #define fourccWAVE 'EVAW'
      #define fourccXWMA 'AMWX'
      #define fourccDPDS 'sdpd'
      #endif
      HRESULT FindChunk(HANDLE hFile, DWORD fourcc, DWORD&amp; dwChunkSize, DWORD&amp; dwChunkDataPosition)
      {
          HRESULT hr = S_OK;
          if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_BEGIN))
              return HRESULT_FROM_WIN32(GetLastError());
  
          DWORD dwChunkType;
          DWORD dwChunkDataSize;
          DWORD dwRIFFDataSize = 0;
          DWORD dwFileType;
          DWORD bytesRead = 0;
          DWORD dwOffset = 0;
  
          while (hr == S_OK)
          {
              DWORD dwRead;
              if (0 == ReadFile(hFile, &amp;dwChunkType, sizeof(DWORD), &amp;dwRead, NULL))
                  hr = HRESULT_FROM_WIN32(GetLastError());
  
              if (0 == ReadFile(hFile, &amp;dwChunkDataSize, sizeof(DWORD), &amp;dwRead, NULL))
                  hr = HRESULT_FROM_WIN32(GetLastError());
  
              switch (dwChunkType)
              {
              case fourccRIFF:
                  dwRIFFDataSize = dwChunkDataSize;
                  dwChunkDataSize = 4;
                  if (0 == ReadFile(hFile, &amp;dwFileType, sizeof(DWORD), &amp;dwRead, NULL))
                      hr = HRESULT_FROM_WIN32(GetLastError());
                  break;
  
              default:
                  if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, dwChunkDataSize, NULL, FILE_CURRENT))
                      return HRESULT_FROM_WIN32(GetLastError());
              }
  
              dwOffset += sizeof(DWORD) * 2;
  
              if (dwChunkType == fourcc)
              {
                  dwChunkSize = dwChunkDataSize;
                  dwChunkDataPosition = dwOffset;
                  return S_OK;
              }
  
              dwOffset += dwChunkDataSize;
  
              if (bytesRead &gt;= dwRIFFDataSize) return S_FALSE;
  
          }
  
          return S_OK;
  
      }
      HRESULT ReadChunkData(HANDLE hFile, void* buffer, DWORD buffersize, DWORD bufferoffset)
      {
          HRESULT hr = S_OK;
          if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, bufferoffset, NULL, FILE_BEGIN))
              return HRESULT_FROM_WIN32(GetLastError());
          DWORD dwRead;
          if (0 == ReadFile(hFile, buffer, buffersize, &amp;dwRead, NULL))
              hr = HRESULT_FROM_WIN32(GetLastError());
          return hr;
      }
  
  
      class VoiceCallback : public IXAudio2VoiceCallback
      {
      public:
          void STDMETHODCALLTYPE OnStreamEnd() override
          {}
          void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() override
          {}
          void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) override
          {}
          void STDMETHODCALLTYPE OnBufferEnd(void* pBufferContext) override
          {
          
              std::cout &lt;&lt; &quot;buffer end&quot; &lt;&lt; std::endl;
          }
          void STDMETHODCALLTYPE OnBufferStart(void* pBufferContext) override
          {
              std::cout &lt;&lt; &quot;buffer start&quot; &lt;&lt; std::endl;
          }
          void STDMETHODCALLTYPE OnLoopEnd(void* pBufferContext) override
          {}
          void STDMETHODCALLTYPE OnVoiceError(void* pBufferContext, HRESULT Error) override
          {}
      };
  
      int main()
      {
  
  
          WAVEFORMATEXTENSIBLE wfx = { 0 };
          XAUDIO2_BUFFER buffer = { 0 };
          HANDLE hFile = CreateFileA(
              &quot;meow.wav&quot;,
              GENERIC_READ,
              FILE_SHARE_READ,
              NULL,
              OPEN_EXISTING,
              0,
              NULL);
  
          if (INVALID_HANDLE_VALUE == hFile)
              return HRESULT_FROM_WIN32(GetLastError());
  
          if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_BEGIN))
              return HRESULT_FROM_WIN32(GetLastError());
  
          DWORD dwChunkSize;
          DWORD dwChunkPosition;
          //check the file type, should be fourccWAVE or 'XWMA'
          FindChunk(hFile, fourccRIFF, dwChunkSize, dwChunkPosition);
          DWORD filetype;
          ReadChunkData(hFile, &amp;filetype, sizeof(DWORD), dwChunkPosition);
          if (filetype != fourccWAVE)
              return S_FALSE;
          FindChunk(hFile, fourccFMT, dwChunkSize, dwChunkPosition);
          ReadChunkData(hFile, &amp;wfx, dwChunkSize, dwChunkPosition);
          //fill out the audio data buffer with the contents of the fourccDATA chunk
          FindChunk(hFile, fourccDATA, dwChunkSize, dwChunkPosition);
          BYTE* pDataBuffer = new BYTE[dwChunkSize];
          ReadChunkData(hFile, pDataBuffer, dwChunkSize, dwChunkPosition);
          buffer.AudioBytes = dwChunkSize;  //size of the audio buffer in bytes
          buffer.pAudioData = pDataBuffer;  //buffer containing audio data
          buffer.Flags = XAUDIO2_END_OF_STREAM; // tell the source voice not to expect any data after this buffer
  
  
  
          HRESULT hr;
          VoiceCallback vcb;
  
          // initialize COM
          if (FAILED(hr = CoInitializeEx(nullptr, COINITBASE_MULTITHREADED)))
          {
              return hr;
          }
  
          // audio engine initialized to nullptr
          IXAudio2* audioEngine = nullptr;
          // initialize audio engine
          if ( FAILED(hr = XAudio2Create(&amp;audioEngine, 0)))
          {
              return hr;
          }
  
          // create mastering voice
          IXAudio2MasteringVoice* masteringVoice = nullptr;
          // initialize masterVoice
          if (FAILED(hr = audioEngine-&gt;CreateMasteringVoice(&amp;masteringVoice)))
          {
              return hr;
          }
  
  
          // create source voice
          IXAudio2SourceVoice* sourceVoice = nullptr;
          // init source voice
          if (FAILED(hr = audioEngine-&gt;CreateSourceVoice(&amp;sourceVoice, (WAVEFORMATEX*)&amp;wfx, 0u, 2.0f, &amp;vcb)))
          {
              return hr;
          }
          // submit audio buffer to source voice
          if (FAILED(hr = sourceVoice-&gt;SubmitSourceBuffer(&amp;buffer)))
          {
              return hr;
          }
  
  
          bool over = false;
          while (!over)
          {
              if (GetAsyncKeyState('S'))
              {
                  over = true;
              }
              if (GetAsyncKeyState('M'))
              {
  
  
                  if (FAILED(hr = sourceVoice-&gt;Start(0)))
                  {
                      return hr;
                  }
              
  
              }
              if (GetAsyncKeyState('A'))
              {
                  sourceVoice-&gt;Stop();
              }
     
          }
  
  
          audioEngine-&gt;Release();
          audioEngine = nullptr;
  
          masteringVoice = nullptr;
  
          CoUninitialize();
  
  
      }
  </code></pre>
  <p>any help would be apreciated.</p>