添加头文件
#include <MMSystem.h>
#pragma comment(lib, "winmm.lib")
1.如果将声音集成到程序中, 需要在添加资源中, 添加您的音乐, 导入程序然后用播放音乐函数:
PlaySound(MAKEINTRESOURCE(IDR_WAVE**), GetModuleHandle(NULL), SND_RESOURCE)//IDR_WAVE**是您导入资源后的ID号
2. 不集成到程序中(输入目录调用播放)
PlaySound("d://***.wav", NULL, SND_FILENAME)//参数一是文件路径
如果要等音乐播放完在往下执行添加标志位: SND_SYNC 就可以, 默认是一边播放一边往下执行
源程序清单如下: LPSTR szFileName//声音文件名 MMCKINFO mmckinfoParentMMCKINFO mmckinfoSubChunkDWORD dwFmtSizeHMMIO m_hmmio//音频文件句柄 DWORD m_WaveLongHPSTR lpData//音频数据 HANDLE m_hDataHANDLE m_hFormatWAVEFORMATEX * lpFormatDWORD m_dwDataOffsetDWORD m_dwDataSizeWAVEHDR pWaveOutHdrWAVEOUTCAPS pwocHWAVEOUT hWaveOut//打开波形文件 if(!(m_hmmio=mmioOpen(szFileName,NULL,MMIO_READ|MMIO_ALLOCBUF))) { //File open Error Error("Failed to open the file.")//错误处理函数 return false} //检查打开文件是否是声音文件 mmckinfoParent.fccType =mmioFOURCC('W','A','V','E')if(mmioDescend(m_hmmio,(LPMMCKINFO)&mmckinfoParent,NULL,MMIO_FINDRIFF)) { //NOT WAVE FILE AND QUIT } //寻找 'fmt' 块 mmckinfoSubChunk.ckid =mmioFOURCC('f','m','t',' ')if(mmioDescend(m_hmmio,&mmckinfoSubChunk,&mmckinfoParent,MMIO_FINDCHUNK)) { //Can't find 'fmt' chunk } //获得 'fmt '块的大小,申请内存 dwFmtSize=mmckinfoSubChunk.cksize m_hFormat=LocalAlloc(LMEM_MOVEABLE,LOWORD(dwFmtSize))if(!m_hFormat) { //failed alloc memory } lpFormat=(WAVEFORMATEX*)LocalLock(m_hFormat)if(!lpFormat) { //failed to lock the memory } if((unsigned long)mmioRead(m_hmmio,(HPSTR)lpFormat,dwFmtSize)!=dwFmtSize) { //failed to read format chunk } //离开 fmt 块 mmioAscend(m_hmmio,&mmckinfoSubChunk,0)//寻找 'data' 块 mmckinfoSubChunk.ckid=mmioFOURCC('d','a','t','a')if(mmioDescend(m_hmmio,&mmckinfoSubChunk,&mmckinfoParent,MMIO_FINDCHUNK)) { //Can't find 'data' chunk } //获得 'data'块的大小 m_dwDataSize=mmckinfoSubChunk.cksize m_dwDataOffset =mmckinfoSubChunk.dwDataOffset if(m_dwDataSize==0L) { //no data in the 'data' chunk } //为音频数据分配内存 lpData=new char[m_dwDataSize]if(!lpData) { //faile } if(mmioSeek(m_hmmio,SoundOffset,SEEK_SET)<0) { //Failed to read the data chunk } m_WaveLong=mmioRead(m_hmmio,lpData,SoundLong)if(m_WaveLong<0) { //Failed to read the data chunk } //检查音频设备,返回音频输出设备的性能 if(waveOutGetDeVCaps(WAVE_MAPPER,&pwoc,sizeof(WAVEOUTCAPS))!=0) { //Unable to allocate or lock memory } //检查音频输出设备是否能播放指定的音频文件 if(waveOutOpen(&hWaveOut,DevsNum,lpFormat,NULL,NULL,CALLBACK_NULL)!=0) { //Failed to OPEN the wave out devices } //准备待播放的数据 pWaveOutHdr.lpData =(HPSTR)lpDatapWaveOutHdr.dwBufferLength =m_WaveLongpWaveOutHdr.dwFlags =0if(waveOutPrepareHeader(hWaveOut,&pWaveOutHdr,sizeof(WAVEHDR))!=0) { //Failed to prepare the wave data buffer } //播放音频数据文件 if(waveOutWrite(hWaveOut,&pWaveOutHdr,sizeof(WAVEHDR))!=0) { //Failed to write the wave data buffer } //关闭音频输出设备,释放内存 waveOutReset(hWaveOut)waveOutClose(hWaveOut)LocalUnlock(m_hFormat)LocalFree(m_hFormat)delete [] lpData用 Beep 函数。BOOL WINAPI Beep(
__in DWORD dwFreq,
__in DWORD dwDuration
)
Beep( 发音频率,发音时间长度)
频率 0x25 到 0x7FFF
发音时间长度 -- 以毫秒计。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)