Method 1: the simplest method
Use repeated playback parameters:Mci_dgv_play_repeat
Mcisendcommand (m_ndeviceid, mci_play,
Mci_dgv_play_repeat, (DWORD) & mciplay ))
Header files required:
# 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 "failed to load background music! ") ;}} Void cmydlg: playbackmusic (void) {mci_play_parms mciplay; If (mcisendcommand (m_ndeviceid, mci_play, publish, (DWORD) & mciplay )) {MessageBox (L "failed to play background music! ");}}
Method 2: replay the sound by responding to the Message mm_mcinotify. For more information, see here: http://www.vckbase.com/document/viewdoc? Id = 633 I modified his Code . Use the mci_seek command to reset the playing position to the beginning, and then call the mci_play command to start playing from the beginning. This saves the extra overhead of opening and closing a file.
/// Mciplaymusic. h/*** @ file mciplaymusic. H * @ author swim blue sea * @ mail you_lan_hai@foxmail.com * @ data 2011-11-30 * Description: * freely modified "how to play large WAV Files?" * By hermess * http://www.vckbase.com/document/viewdoc? Id = 633 */# pragma once # include <mmsystem. h>/** play music class */class cmciplaymusic {public: // constructor cmciplaymusic (); Virtual ~ Cmciplaymusic (); Public: DWORD opendevice (); DWORD closedevice (); DWORD play (cwnd * pparentwnd, lpctstr pfilename); DWORD stop (); DWORD seektostart (); lpctstr geterrormsg (DWORD dwerror); Response getdeviceid (void) {return m_ndeviceid;} mcideviceid getelementid (void) {return m_nelementid;} protected: protected entity; required 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_t Ype_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 I F 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_policy, (DWORD) (lpvoid) & mciplayparms ); // Display Error and close element if Fai Led if (dwresult! = 0) // failed {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) {eclipseekparam; zeromemory (& seekparam, sizeof (seekparam); dwresult = mcisendcommand (m_nelementid, entity, entity, (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 m Essage if (! Mcigeterrorstring (dwerror, szerrormsg, sizeof (szerrormsg) {lstrcpy (szerrormsg, _ T ("Unknown error. ") ;}} Else {lstrcpy (szerrormsg, _ T (" no error. ");} Return szerrormsg ;}
Add a Response Message to the window:On_message (mm_mcinotify, & cmydlg: onmcinotify)
Lresult cmydlg: onmcinotify (wparam, lparam) {If (wparam = callback) {playbackmusic ();} return 0;} void cmydlg: openmcideveci (void) {DWORD dwresult = 0; dwresult = m_mcimusic.opendevice (); If (0! = Dwresult) {MessageBox (callback (dwresult) ;}} void cmyllkdlg: playbackmusic (void) {callback (); // m_mcimusic.stop (); DWORD dwresult = m_mcimusic.play (this, _t({your_music.pdf "); If (dwresult! = 0) {MessageBox (m_mcimusic.geterrormsg (dwresult ));}}