方法一:最简单的方法
使用重复播放参数:MCI_DGV_PLAY_REPEAT
mciSendCommand(m_nDeviceID, MCI_PLAY, MCI_DGV_PLAY_REPEAT, (DWORD)&mciPlay))
需要头文件:
- #include <Digitalv.h>
- #include <MMSystem.h>
- #pragma comment(lib , "winmm.lib")
- 。。。。。。
- MCIDEVICEID m_nDeviceID;
- 。。。。。
- void CMyDlg::OpenMciDeveci(void)
- {
- DWORD dwResult = 0;
- MCI_OPEN_PARMS mciOpenParms;
- mciOpenParms.lpstrDeviceType = _T("sequencer");
- mciOpenParms.lpstrElementName = _T(SOUND_BACK);
- dwResult = mciSendCommand(NULL,
- MCI_OPEN,
- MCI_OPEN_ELEMENT,
- (DWORD)&mciOpenParms);
- //save device identifier,will use eith other MCI commands
- m_nDeviceID = mciOpenParms.wDeviceID;
- if (dwResult != 0)
- {
- MessageBox(L"加载背景音乐失败!");
- }
- }
- void CMyDlg::PlayBackMusic(void)
- {
- MCI_PLAY_PARMS mciPlay;
- if(mciSendCommand(m_nDeviceID, MCI_PLAY, MCI_DGV_PLAY_REPEAT, (DWORD)&mciPlay))
- {
- MessageBox(L"播放背景音乐失败!");
- }
- }
我把他的代码改装了下。利用MCI_SEEK命令将播放位置重置到开头,然后再调用MCI_PLAY命令,即可从头开始播放。这样就省去了打开关闭文件时带来的额外开销。
- ///MCIPlayMusic.h
- /**
- * @file MCIPlayMusic.h
- * @author 游蓝海
- * @mail you_lan_hai@foxmail.com
- * @data 2011-11-30
- * 说明:
- * 改装自如《何播放大型 WAV 文件?》
- * 原作者:hermess
- * http://www.vckbase.com/document/viewdoc/?id=633
- */
- #pragma once
- #include <MMSystem.h>
- /**播放音乐类*/
- class cMciPlayMusic
- {
- public:
- ///构造函数
- cMciPlayMusic();
- virtual ~cMciPlayMusic();
- public:
- DWORD openDevice();
- DWORD closeDevice();
- DWORD play(CWnd *pParentWnd,LPCTSTR pFileName);
- DWORD stop();
- DWORD seekToStart();
- LPCTSTR getErrorMsg(DWORD dwError);
- MCIDEVICEID getDeviceID(void){ return m_nDeviceID; }
- MCIDEVICEID getElementID(void){ return m_nElementID; }
- protected:
- MCIDEVICEID m_nDeviceID;
- MCIDEVICEID m_nElementID;
- };
- #include <stdafx.h>
- #include "MCIPlayMusic.h"
- cMciPlayMusic::cMciPlayMusic()
- {
- m_nDeviceID=0;
- m_nElementID=0;
- }
- cMciPlayMusic::~cMciPlayMusic()
- {
- if(m_nElementID != 0)
- {
- stop();
- }
- if(m_nDeviceID != 0)
- {
- closeDevice();
- }
- }
- DWORD cMciPlayMusic::openDevice()
- {
- DWORD dwResult=0;
- if (m_nDeviceID == 0)
- {
- MCI_OPEN_PARMS mciOpenParms;
- ZeroMemory(&mciOpenParms, sizeof(mciOpenParms));
- //mciOpenParms.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
- mciOpenParms.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_SEQUENCER;
- //open the wave device
- dwResult = mciSendCommand(NULL,
- MCI_OPEN,
- MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT,
- (DWORD)(LPVOID)&mciOpenParms);
- //save device identifier,will use eith other MCI commands
- m_nDeviceID = mciOpenParms.wDeviceID;
- }
- //return result of MCI operation
- return dwResult;
- }
- DWORD cMciPlayMusic::closeDevice()
- {
- DWORD dwResult=0;
- //close if currently open
- if(m_nDeviceID)
- {
- //close the MCI device
- dwResult=mciSendCommand(m_nDeviceID,MCI_CLOSE,NULL,NULL);
- //display error message if failed
- if(!dwResult)
- {
- m_nDeviceID=0;
- }
- }
- //return result of MCI operation
- return dwResult;
- }
- DWORD cMciPlayMusic::play(CWnd* pWnd,LPCTSTR pFileName)
- {
- if (m_nElementID == 0)
- {
- MCI_OPEN_PARMS mciOpenParms;
- //initialize structure
- memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS));
- //set the WAV file name to be played
- mciOpenParms.lpstrElementName = pFileName;
- //first open the device
- DWORD dwResult = mciSendCommand(m_nDeviceID,MCI_OPEN,
- MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);
- if (dwResult != 0)
- {
- return dwResult;
- }
- else
- {
- //save element indentifier
- m_nElementID = mciOpenParms.wDeviceID;
- }
- }
- MCI_PLAY_PARMS mciPlayParms;
- //set the window that will receive notification message
- mciPlayParms.dwCallback = (DWORD)pWnd->m_hWnd;
- //instruct device to play file
- DWORD dwResult=mciSendCommand(m_nElementID,MCI_PLAY,
- MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
- //display error and close element if failed
- if(dwResult != 0)//失败
- {
- stop();
- }
- //return result of MCI operation
- return dwResult;
- }
- DWORD cMciPlayMusic::stop()
- {
- DWORD dwResult=0;
- //close if element is currently open
- if(m_nElementID != 0)
- {
- dwResult=mciSendCommand(m_nElementID,MCI_CLOSE,NULL,NULL);
- //display error message if failed
- if(dwResult == 0)
- {
- m_nElementID=0;
- }
- }
- return dwResult;
- }
- DWORD cMciPlayMusic::seekToStart()
- {
- DWORD dwResult=0;
- //close if element is currently open
- if(m_nElementID != 0)
- {
- MCI_SEEK_PARMS seekParam;
- ZeroMemory(&seekParam, sizeof(seekParam));
- dwResult=mciSendCommand(m_nElementID,
- MCI_SEEK,
- MCI_SEEK_TO_START,
- (DWORD)&seekParam);
- }
- return dwResult;
- }
- LPCTSTR cMciPlayMusic::getErrorMsg(DWORD dwError)
- {
- //character string that contains error message
- static TCHAR szErrorMsg[MAXERRORLENGTH];
- //check if there was an error
- if(dwError)
- {
- //retrieve string associated error message
- if(!mciGetErrorString(dwError,szErrorMsg,sizeof(szErrorMsg)))
- {
- lstrcpy(szErrorMsg,_T("未知错误。"));
- }
- }
- else
- {
- lstrcpy(szErrorMsg, _T("没有错误。"));
- }
- return szErrorMsg;
- }
给窗口添加响应消息: ON_MESSAGE(MM_MCINOTIFY, &CMyDlg::OnMCINotify)
- LRESULT CMyDlg::OnMCINotify(WPARAM wParam, LPARAM lParam)
- {
- if (wParam == MCI_NOTIFY_SUCCESSFUL )
- {
- PlayBackMusic();
- }
- return 0;
- }
- void CMyDlg::OpenMciDeveci(void)
- {
- DWORD dwResult = 0;
- dwResult = m_mciMusic.openDevice();
- if(0 != dwResult)
- {
- MessageBox(m_mciMusic.getErrorMsg(dwResult));
- }
- }
- void CMyLLKDlg::PlayBackMusic(void)
- {
- m_mciMusic.seekToStart();
- //m_mciMusic.stop();
- DWORD dwResult = m_mciMusic.play(this, _T(“your_music.mp3”));
- if (dwResult != 0)
- {
- MessageBox(m_mciMusic.getErrorMsg(dwResult));
- }
- }