WAVE play

Basic WAVE playback process

1. Inquire whether the system has an output device for playing waveform audio, call the waveOutGetNumDevs function to get the number of devices;

2. Read the waveform audio data for playback from the WAV file;

3. Check the performance of the WAVE output device to determine that the device supports the current WAV file format, and call waveOutGetDevCaps to complete this function;

4. Open the wave audio output device and set the callback object, call the waveOutOpen function;

5. Construct and fill WAVE audio information structure WAVEHDR;

6. Write the prepared WAVEHDR to the waveform audio output device and call the waveOutWrite function;

7. Clear the prepared wave audio data block WAVEHDR and release system resources, call the waveOutUnprepareHeader function;

8. Close the opened wave audio output device, call the waveOutClose function to close the audio output device. Note that if it is currently playing, you should call the waveOutReset function to terminate the playback, and then turn off the device, otherwise the shutdown will fail. The waveOutReset function is responsible for discarding the unprocessed buffer and returning it to the program, while resetting the output. You can also call the waveOutPause function to pause the current playback, and use the waveOutRestart function to resume playback from the current paused position.

Precautions

1. When using the mmioRead function to read WAV file data, after the file pointer reaches the end, or when the read data is not enough, even if the buffer is initialized, it will fill with data randomly. Therefore, pHdr->dwBufferLength cannot be greater than the real data length, otherwise it will cause noise when playing the last data block.
2. When using the callback function in the waveOutOpen function, pay attention to the deadlock problem of waveOutReset.
3. When calling the waveOutWrite function to play audio data, you need to use the double buffer mechanism, otherwise reading a section and playing a section will cause a pause, which will cause noise.
4. When releasing the prepared Header in the callback function, the following safety methods should be used:
if ((m_pArrHdr[i]->dwFlags & (WHDR_DONE | WHDR_PREPARED)) != 0)
{
	while (waveOutUnprepareHeader(m_hWaveOut, m_pArrHdr[i], sizeof(WAVEHDR)) 
		== WAVERR_STILLPLAYING)
	{
		Sleep(1);
	}
}
5. In the callback function, the PostMessage asynchronous function should be used to end the playback, and the waveOutClose function should not be used directly.


Guess you like

Origin blog.csdn.net/gkzscs/article/details/53978442