VC学习笔记6音频处理

音频处理
首先在资源视图右击导入音频资源,记下ID为IDR_BWAVE
在视图类的源文件中需要导入库,该库为windows多媒体操作的接口
#include"Mmsystem.h"  
#pragma comment (lib,"Winmm.lib")
然后在所需要播放音频的响应函数中加入
PlaySound(MAKEINTRESOURCE(IDR_BWAVE),AfxGetResourceHandle(),
      SND_ASYNC|SND_RESOURCE|SND_NODEFAULT);
就能实现播放音频
当然,如果你想播放任意文件名为参数的音频,可以调用这个类
class CPlayWave
{
protected:
    MCIDEVICEID m_DeviceID;  //设备ID
    MCIDEVICEID m_ElementID;  //元素ID
public:
    CPlayWave()
    {
        m_DeviceID=0;
        m_ElementID=0;
        InitDevice();
    }
    ~CPlayWave()
    {
        if(m_ElementID)
            Stop();
        if(m_DeviceID)
            TerminalDevice();
    }
    DWORD InitDevice()  //初始化设备
    {
        DWORD dwResult=0;
        if(m_DeviceID==0)
        {
            MCI_OPEN_PARMS mciInitParms;
            mciInitParms.lpstrDeviceType=(LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
            dwResult=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT,(DWORD)(LPVOID)&mciInitParms);
            m_DeviceID=mciInitParms.wDeviceID;
        }
        return dwResult;
    }
    DWORD TerminalDevice()
    {
        DWORD dwResult=0;
        if(m_DeviceID)
        {
            dwResult=mciSendCommand(m_DeviceID,MCI_CLOSE,NULL,NULL);
        }
        m_DeviceID=0;
        return dwResult;
    }
    DWORD Play(HWND hWnd,LPCSTR pFileName)
    {
        MCI_OPEN_PARMS mciOpenParms;
        memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS));
        mciOpenParms.lpstrElementName=pFileName;
        DWORD dwResult=0;
        dwResult=mciSendCommand(m_DeviceID,MCI_OPEN,MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);
        if(!dwResult)
        {
            m_ElementID=mciOpenParms.wDeviceID;
            MCI_PLAY_PARMS mciPlayParms;
            mciPlayParms.dwCallback=(DWORD)hWnd;
            dwResult=mciSendCommand(m_ElementID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
        }
        return dwResult;
    }
    DWORD Stop()
    {
        DWORD dwResult=0;
        if(m_ElementID)
        {
            dwResult=mciSendCommand(m_ElementID,MCI_CLOSE,NULL,NULL);
            m_ElementID=0;
        }
        return dwResult;
    }
};



注意了这里的调入参数为m_hWnd ,每个程序都会自动生成变量m_hWnd

对wav文件格式的解析可以参考

http://blog.csdn.net/pi9nc/article/details/12570841





你可能感兴趣的:(VC学习笔记6音频处理)