📄 playfile.cpp
字号:
// Playfile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#define MM_WOM_SETSECONDARYGAINCLASS (WM_USER)
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwWait;
WAVEFORMATEX m_wfx;
HWAVEOUT m_hWaveOutDevice;
WAVEHDR *l_pCurrent;
HANDLE hevDone = CreateEvent(NULL, FALSE, FALSE, NULL);
HANDLE m_hHeap = GetProcessHeap();
MMRESULT mmRetCode;
//the data format of "poweroff.wav"
ZeroMemory(&m_wfx,sizeof(m_wfx));
m_wfx.cbSize = 0;
m_wfx.wFormatTag = WAVE_FORMAT_PCM;
m_wfx.wBitsPerSample = 8;// default to 8-bit samples
m_wfx.nSamplesPerSec = 8000;
m_wfx.nChannels = 1;
m_wfx.nBlockAlign = (m_wfx.nChannels * m_wfx.wBitsPerSample) >> 3;
m_wfx.nAvgBytesPerSec = m_wfx.nBlockAlign * m_wfx.nSamplesPerSec;
//1.Open the file:
HANDLE l_hFile = NULL;
int l_iFileSize = 0;
BYTE *l_pFileStart;
DWORD l_dwReadBytes;
l_hFile = CreateFile( L"test.wav",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
l_iFileSize = GetFileSize(l_hFile,NULL);
l_pFileStart = (BYTE*) HeapAlloc( m_hHeap, 0 ,l_iFileSize );
ReadFile(l_hFile,l_pFileStart,l_iFileSize,&l_dwReadBytes,NULL);
//2.open the wave stream.
mmRetCode = waveOutOpen(&m_hWaveOutDevice,
WAVE_MAPPER,
&m_wfx,
(DWORD)hevDone,
NULL,
CALLBACK_EVENT);
//3.set the secondary gain class
waveOutMessage(m_hWaveOutDevice,MM_WOM_SETSECONDARYGAINCLASS,2,0);
//4. fill the buffer from the file.
l_pCurrent = (LPWAVEHDR) HeapAlloc(m_hHeap,HEAP_ZERO_MEMORY, sizeof(WAVEHDR) );
l_pCurrent->lpData = (LPSTR)l_pFileStart;
l_pCurrent->dwBufferLength = l_iFileSize;
//memcpy(l_pCurrent->lpData, l_pFileStart, l_iFileSize);
//5. write the buffer to audio driver.
MMRESULT l_res = waveOutPrepareHeader(m_hWaveOutDevice, l_pCurrent, sizeof(WAVEHDR));
MMRESULT l_res1 = waveOutWrite(m_hWaveOutDevice, l_pCurrent, sizeof(WAVEHDR));
//6. wait for end of the playing
dwWait = WaitForSingleObject(hevDone, 5000);//5000: timeout value, just magic number.
//7.close the wave stream.
waveOutClose(m_hWaveOutDevice);
CloseHandle(l_hFile);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -