Original Post
I''ve written a class that loads a wave file into memory, takes the necessary information from it to fill a WAVEFORMATEX structure and when a function is called, a waveOut device is opened and the file is played. The file plays fine, however, when the file is finished playing, I get an access violation. The code for playing the file is below:
I''ve tried everything. The code above has the callback function taken out to see if it was that, which it wasn''t. The access violation isn''t caused by the buffer being too small, and there are no problems that I can see in the WAVEFORMATEX structure or WAVEHDR structure.
Any ideas? Your help will be greatly appreciated.
bool CWaveFile::PlayWave()
{
HWAVEOUT hWaveOut;
WAVEHDR whdr;
if (!this->data) {
return false;
}
if (waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfex, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR) {
return false;
}
VirtualLock(this->data, this->dwDataSize);
memset(&whdr, 0, sizeof(WAVEHDR));
whdr.lpData = this->data;
whdr.dwBufferLength = this->dwDataSize;
whdr.dwFlags = 0;
whdr.dwLoops = 1;
whdr.dwBytesRecorded = 0;
whdr.dwUser = 0;
if (waveOutPrepareHeader(hWaveOut, &whdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
VirtualUnlock(this->data, this->dwDataSize);
waveOutClose(hWaveOut);
return false;
}
whdr.dwFlags |= WHDR_BEGINLOOP | WHDR_ENDLOOP;
if (waveOutWrite(hWaveOut, &whdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
VirtualUnlock(this->data, this->dwDataSize);
waveOutClose(hWaveOut);
return false;
}
return true;
}