The issue is that even after the sound is loaded into memory, I cannot hear it after I start the sound. Also, I run into an 'Access violation reading location' error.
I have tried to debug and noticed that the sound is loaded successfully and the Start function returns an S_OK too. The unhandled exception error takes place after the Start function and no sound is heard.
This is the error I receive....
Unhandled exception at 0x5f7d032d in SoundManagerTest.exe: 0xC0000005: Access violation reading location 0x9a3a99d8.
And the following is the code
SoundManager.h
/** Sound Manager v 0.1 - To handle all sound related tasks* Coded by - Andy Dbest*/#ifndef _SOUND_MANAGER#define _SOUND_MANAGER#include <XAudio2.h>#include <windows.h>#include <iostream>using namespace std;namespace GameZombies { #define fourccRIFF 'FFIR' #define fourccDATA 'atad' #define fourccFMT ' tmf' #define fourccWAVE 'EVAW' #define fourccXWMA 'AMWX' #define fourccDPDS 'sdpd' class SoundManager { private: IXAudio2* pXAudio2; //core of the XAudio2 engine IXAudio2MasteringVoice* pMasterVoice; //encapsulates an audio device, //and is the ultimate destination for all audio UINT32 flags; //flag to set while creating XAudio2 interface XAUDIO2_BUFFER Buffer; IXAudio2SourceVoice* pSourceVoice; HRESULT FindChunk(HANDLE, DWORD, DWORD &, DWORD &); //To find a chunk in a RIFF file HRESULT ReadChunkData(HANDLE , void *, DWORD, DWORD); //To read data in a chunk after it has been located public: SoundManager(void); ~SoundManager(void); bool InitializeSound(); int LoadSound(LPWSTR); HRESULT Play(); };}#endif
SoundManager.cpp
#include "SoundManager.h"using namespace GameZombies;#ifndef SAFE_RELEASE#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }#endif//DestructorSoundManager::~SoundManager(void){ // All XAudio2 interfaces are released when the engine is destroyed, but being tidy if (pSourceVoice != NULL) { pSourceVoice->Stop(0); pSourceVoice->DestroyVoice(); } pMasterVoice->DestroyVoice(); SAFE_RELEASE( pXAudio2 ); CoUninitialize();}//ConstructorSoundManager::SoundManager(void){ pXAudio2 = NULL; pMasterVoice = NULL; flags = 0; CoInitializeEx(NULL, COINIT_MULTITHREADED); cout<<"COM initialized\n";}bool SoundManager::InitializeSound(){ HRESULT hr; if (FAILED(hr = XAudio2Create( &pXAudio2, flags))) { cout<<"Failed to create Audio2\n"; return false; } if (FAILED(hr = pXAudio2->CreateMasteringVoice(&pMasterVoice))) { cout<<"Failed to create MasteringVoice\n"; return false; } cout<<"SoundManager Initialized Successfully\n"; return true;}int SoundManager::LoadSound(LPWSTR fileName){ WAVEFORMATEXTENSIBLE wfx = {0}; //defines the format of the audio file //Open the file HANDLE hFile = CreateFile(fileName,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() ); //Locate the 'RIFF' chunk in the audio file, and check the file type DWORD dwChunkSize; DWORD dwChunkPosition; //check the file type, should be fourccWAVE or 'XWMA' FindChunk(hFile,fourccRIFF,dwChunkSize, dwChunkPosition ); DWORD filetype; ReadChunkData(hFile,&filetype,sizeof(DWORD),dwChunkPosition); if (filetype != fourccWAVE) return S_FALSE; //Locate the 'fmt ' chunk, and copy its contents into a WAVEFORMATEXTENSIBLE structure FindChunk(hFile,fourccFMT, dwChunkSize, dwChunkPosition ); ReadChunkData(hFile, &wfx, dwChunkSize, dwChunkPosition ); //Locate the 'data' chunk, and read its contents into a buffer FindChunk(hFile,fourccDATA,dwChunkSize, dwChunkPosition ); BYTE * pDataBuffer = new BYTE[dwChunkSize]; ReadChunkData(hFile, pDataBuffer, dwChunkSize, dwChunkPosition); //Populate the XAudio2 Buffer Buffer.AudioBytes = dwChunkSize; Buffer.pAudioData = pDataBuffer; Buffer.Flags = XAUDIO2_END_OF_STREAM; HRESULT hr; //Create the source voice if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&wfx))) { cout<<"Error while creating source voice\n"; return false; } if( FAILED(hr = pSourceVoice->SubmitSourceBuffer( &Buffer ) ) ) return hr; cout<<"Sound Loaded Successfully\n"; return S_OK;}HRESULT SoundManager::Play(){ HRESULT hr; if ( FAILED(hr = pSourceVoice->Start( 0 ) ) ) return hr; cout<<"Sound Playing\n";<span class="cpp-comment">//ERROR OCCURS HERE</span><br> <span class="cpp-keyword">return</span> hr;<br>}<br><br>HRESULT SoundManager::FindChunk(HANDLE hFile, DWORD fourcc, DWORD & dwChunkSize, DWORD & dwChunkDataPosition)<br>{<br> HRESULT hr = S_OK;<br> <span class="cpp-keyword">if</span>( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, <span class="cpp-number">0</span>, NULL, FILE_BEGIN ) )<br> <span class="cpp-keyword">return</span> HRESULT_FROM_WIN32( GetLastError() );<br><br> DWORD dwChunkType;<br> DWORD dwChunkDataSize;<br> DWORD dwRIFFDataSize = <span class="cpp-number">0</span>;<br> DWORD dwFileType;<br> DWORD bytesRead = <span class="cpp-number">0</span>;<br> DWORD dwOffset = <span class="cpp-number">0</span>;<br><br> <span class="cpp-keyword">while</span> (hr == S_OK)<br> {<br> DWORD dwRead;<br> <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, &dwChunkType, <span class="cpp-keyword">sizeof</span>(DWORD), &dwRead, NULL ) )<br> hr = HRESULT_FROM_WIN32( GetLastError() );<br><br> <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, &dwChunkDataSize, <span class="cpp-keyword">sizeof</span>(DWORD), &dwRead, NULL ) )<br> hr = HRESULT_FROM_WIN32( GetLastError() );<br><br> <span class="cpp-keyword">switch</span> (dwChunkType)<br> {<br> <span class="cpp-keyword">case</span> fourccRIFF:<br> dwRIFFDataSize = dwChunkDataSize;<br> dwChunkDataSize = <span class="cpp-number">4</span>;<br> <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, &dwFileType, <span class="cpp-keyword">sizeof</span>(DWORD), &dwRead, NULL ) )<br> hr = HRESULT_FROM_WIN32( GetLastError() );<br> <span class="cpp-keyword">break</span>;<br><br> <span class="cpp-keyword">default</span>:<br> <span class="cpp-keyword">if</span>( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, dwChunkDataSize, NULL, FILE_CURRENT ) )<br> <span class="cpp-keyword">return</span> HRESULT_FROM_WIN32( GetLastError() ); <br> }<br><br> dwOffset += <span class="cpp-keyword">sizeof</span>(DWORD) * <span class="cpp-number">2</span>;<br> <br> <span class="cpp-keyword">if</span> (dwChunkType == fourcc)<br> {<br> dwChunkSize = dwChunkDataSize;<br> dwChunkDataPosition = dwOffset;<br> <span class="cpp-keyword">return</span> S_OK;<br> }<br><br> dwOffset += dwChunkDataSize;<br> <br> <span class="cpp-keyword">if</span> (bytesRead &gt;= dwRIFFDataSize) <span class="cpp-keyword">return</span> S_FALSE;<br><br> }<br><br> <br> <span class="cpp-keyword">return</span> S_OK;<br> <br>}<br><br><br>HRESULT SoundManager::ReadChunkData(HANDLE hFile, <span class="cpp-keyword">void</span> * buffer, DWORD buffersize, DWORD bufferoffset)<br>{<br> HRESULT hr = S_OK;<br> <span class="cpp-keyword">if</span>( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, bufferoffset, NULL, FILE_BEGIN ) )<br> <span class="cpp-keyword">return</span> HRESULT_FROM_WIN32( GetLastError() );<br> DWORD dwRead;<br> <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, buffer, buffersize, &dwRead, NULL ) )<br> hr = HRESULT_FROM_WIN32( GetLastError() );<br> <span class="cpp-keyword">return</span> hr;<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>main.cpp<br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-directive">#include</span> <span class="cpp-literal">"SoundManager.h"</span><br><br><span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> GameZombies;<br><br><span class="cpp-keyword">int</span> main()<br>{<br> SoundManager sound;<br> HRESULT hr;<br> <span class="cpp-comment">//CoInitializeEx(NULL, COINIT_MULTITHREADED);</span><br> <span class="cpp-keyword">if</span> (!sound.InitializeSound())<br> cout &lt;&lt;<span class="cpp-literal">"Error initializing sound\n"</span>;<br> <span class="cpp-keyword">else</span> {<br> <span class="cpp-keyword">if</span> (FAILED(hr = sound.LoadSound(L<span class="cpp-literal">"Mono.wav"</span>))){<br> cout &lt;&lt;<span class="cpp-literal">"Error Loading sound\n"</span>;<br> cout &lt;&lt;hr;<br> }<br> <span class="cpp-keyword">else</span> {<br> sound.Play();<br> }<br> }<br><br> system (<span class="cpp-literal">"pause"</span>);<br><br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>}<br><br></pre></div><!–ENDSCRIPT–>