使用mciSendCommand循环播放音乐

 方法一:最简单的方法

使用重复播放参数:MCI_DGV_PLAY_REPEAT

mciSendCommand(m_nDeviceID, MCI_PLAY, MCI_DGV_PLAY_REPEAT, (DWORD)&mciPlay))

需要头文件:

[cpp]  view plain copy
  1. #include <Digitalv.h>  
  2. #include <MMSystem.h>  
  3. #pragma comment(lib , "winmm.lib")  
  4. 。。。。。。  
[cpp]  view plain copy
  1. MCIDEVICEID m_nDeviceID;  
  2. 。。。。。  
[cpp]  view plain copy
  1. void CMyDlg::OpenMciDeveci(void)  
  2. {  
  3.     DWORD dwResult = 0;  
  4.   
  5.     MCI_OPEN_PARMS mciOpenParms;  
  6.     mciOpenParms.lpstrDeviceType = _T("sequencer");  
  7.     mciOpenParms.lpstrElementName = _T(SOUND_BACK);  
  8.   
  9.   
  10.     dwResult = mciSendCommand(NULL,   
  11.         MCI_OPEN,  
  12.         MCI_OPEN_ELEMENT,  
  13.         (DWORD)&mciOpenParms);  
  14.      
  15.     //save device identifier,will use eith other MCI commands  
  16.     m_nDeviceID = mciOpenParms.wDeviceID;  
  17.     if (dwResult != 0)  
  18.     {  
  19.        MessageBox(L"加载背景音乐失败!");  
  20.     }  
  21.   
  22. }  
  23.   
  24. void CMyDlg::PlayBackMusic(void)  
  25. {  
  26.     MCI_PLAY_PARMS mciPlay;  
  27.     if(mciSendCommand(m_nDeviceID, MCI_PLAY, MCI_DGV_PLAY_REPEAT, (DWORD)&mciPlay))  
  28.     {  
  29.         MessageBox(L"播放背景音乐失败!");  
  30.     }  
  31. }  

        方法二:通过响应MM_MCINOTIFY消息来重复播放声音。详情参考这里:http://www.vckbase.com/document/viewdoc/?id=633
        我把他的代码改装了下。利用MCI_SEEK命令将播放位置重置到开头,然后再调用MCI_PLAY命令,即可从头开始播放。这样就省去了打开关闭文件时带来的额外开销。
[cpp]  view plain copy
  1. ///MCIPlayMusic.h   
  2. /**  
  3. * @file MCIPlayMusic.h  
  4. * @author 游蓝海 
  5. * @mail you_lan_hai@foxmail.com 
  6. * @data 2011-11-30 
  7. * 说明: 
  8. * 改装自如《何播放大型 WAV 文件?》  
  9. * 原作者:hermess 
  10. * http://www.vckbase.com/document/viewdoc/?id=633 
  11. */  
  12. #pragma once  
  13.   
  14. #include <MMSystem.h>  
  15.   
  16. /**播放音乐类*/  
  17. class cMciPlayMusic  
  18. {  
  19. public:  
  20.     ///构造函数  
  21.     cMciPlayMusic();  
  22.     virtual ~cMciPlayMusic();  
  23.   
  24. public:  
  25.     DWORD openDevice();  
  26.     DWORD closeDevice();  
  27.     DWORD play(CWnd *pParentWnd,LPCTSTR pFileName);  
  28.     DWORD stop();  
  29.     DWORD seekToStart();  
  30.   
  31.     LPCTSTR getErrorMsg(DWORD dwError);  
  32.       
  33.     MCIDEVICEID getDeviceID(void){ return m_nDeviceID; }  
  34.     MCIDEVICEID getElementID(void){ return m_nElementID; }  
  35.   
  36. protected:  
  37.     MCIDEVICEID m_nDeviceID;  
  38.     MCIDEVICEID m_nElementID;  
  39. };  

[cpp]  view plain copy
  1. #include <stdafx.h>  
  2. #include "MCIPlayMusic.h"  
  3.   
  4. cMciPlayMusic::cMciPlayMusic()  
  5. {  
  6.     m_nDeviceID=0;  
  7.     m_nElementID=0;  
  8. }  
  9.   
  10. cMciPlayMusic::~cMciPlayMusic()  
  11. {  
  12.     if(m_nElementID != 0)  
  13.     {  
  14.         stop();  
  15.     }  
  16.     if(m_nDeviceID != 0)  
  17.     {  
  18.         closeDevice();  
  19.     }  
  20. }  
  21.   
  22. DWORD cMciPlayMusic::openDevice()  
  23. {  
  24.     DWORD dwResult=0;  
  25.   
  26.     if (m_nDeviceID == 0)  
  27.     {  
  28.         MCI_OPEN_PARMS mciOpenParms;  
  29.         ZeroMemory(&mciOpenParms, sizeof(mciOpenParms));  
  30.   
  31.         //mciOpenParms.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;  
  32.         mciOpenParms.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_SEQUENCER;  
  33.           
  34.         //open the wave device  
  35.         dwResult = mciSendCommand(NULL,  
  36.             MCI_OPEN,  
  37.             MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT,  
  38.             (DWORD)(LPVOID)&mciOpenParms);  
  39.   
  40.         //save device identifier,will use eith other MCI commands  
  41.         m_nDeviceID = mciOpenParms.wDeviceID;  
  42.   
  43.     }  
  44.     //return result of MCI operation  
  45.     return dwResult;  
  46. }  
  47.   
  48. DWORD cMciPlayMusic::closeDevice()  
  49. {  
  50.     DWORD dwResult=0;  
  51.   
  52.     //close if currently open  
  53.     if(m_nDeviceID)  
  54.     {  
  55.         //close the MCI device  
  56.         dwResult=mciSendCommand(m_nDeviceID,MCI_CLOSE,NULL,NULL);  
  57.   
  58.         //display error message if failed  
  59.         if(!dwResult)  
  60.         {  
  61.             m_nDeviceID=0;  
  62.         }  
  63.     }  
  64.   
  65.     //return result of MCI operation  
  66.     return dwResult;  
  67. }  
  68.   
  69. DWORD cMciPlayMusic::play(CWnd* pWnd,LPCTSTR pFileName)  
  70. {  
  71.     if (m_nElementID == 0)  
  72.     {  
  73.         MCI_OPEN_PARMS mciOpenParms;  
  74.         //initialize structure  
  75.         memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS));  
  76.   
  77.         //set the WAV file name to be played  
  78.         mciOpenParms.lpstrElementName = pFileName;  
  79.   
  80.         //first open the device  
  81.         DWORD dwResult = mciSendCommand(m_nDeviceID,MCI_OPEN,  
  82.             MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);  
  83.   
  84.         if (dwResult != 0)  
  85.         {  
  86.             return dwResult;  
  87.         }  
  88.         else  
  89.         {  
  90.             //save element indentifier  
  91.             m_nElementID = mciOpenParms.wDeviceID;  
  92.   
  93.         }  
  94.     }  
  95.   
  96.   
  97.     MCI_PLAY_PARMS mciPlayParms;  
  98.   
  99.     //set the window that will receive notification message  
  100.     mciPlayParms.dwCallback = (DWORD)pWnd->m_hWnd;  
  101.   
  102.     //instruct device to play file  
  103.     DWORD dwResult=mciSendCommand(m_nElementID,MCI_PLAY,  
  104.         MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);  
  105.   
  106.     //display error and close element if failed  
  107.     if(dwResult != 0)//失败  
  108.     {  
  109.         stop();  
  110.     }  
  111.   
  112.     //return result of MCI operation  
  113.     return dwResult;  
  114. }  
  115.   
  116.   
  117. DWORD cMciPlayMusic::stop()  
  118. {  
  119.     DWORD dwResult=0;  
  120.   
  121.     //close if element is currently open  
  122.     if(m_nElementID != 0)  
  123.     {  
  124.         dwResult=mciSendCommand(m_nElementID,MCI_CLOSE,NULL,NULL);  
  125.   
  126.         //display error message if failed  
  127.         if(dwResult == 0)  
  128.         {  
  129.             m_nElementID=0;  
  130.         }  
  131.     }  
  132.     return dwResult;  
  133. }  
  134.   
  135. DWORD cMciPlayMusic::seekToStart()  
  136. {  
  137.     DWORD dwResult=0;  
  138.   
  139.     //close if element is currently open  
  140.     if(m_nElementID != 0)  
  141.     {  
  142.         MCI_SEEK_PARMS seekParam;  
  143.         ZeroMemory(&seekParam, sizeof(seekParam));  
  144.         dwResult=mciSendCommand(m_nElementID,  
  145.             MCI_SEEK,  
  146.             MCI_SEEK_TO_START,  
  147.             (DWORD)&seekParam);  
  148.     }  
  149.     return dwResult;  
  150. }  
  151.   
  152. LPCTSTR cMciPlayMusic::getErrorMsg(DWORD dwError)  
  153. {  
  154.     //character string that contains error message  
  155.     static TCHAR szErrorMsg[MAXERRORLENGTH];  
  156.   
  157.     //check if there was an error  
  158.     if(dwError)  
  159.     {  
  160.         //retrieve string associated error message  
  161.         if(!mciGetErrorString(dwError,szErrorMsg,sizeof(szErrorMsg)))  
  162.         {  
  163.             lstrcpy(szErrorMsg,_T("未知错误。"));  
  164.         }  
  165.     }  
  166.     else  
  167.     {  
  168.         lstrcpy(szErrorMsg, _T("没有错误。"));  
  169.     }  
  170.     return szErrorMsg;  
  171. }     

给窗口添加响应消息: ON_MESSAGE(MM_MCINOTIFY, &CMyDlg::OnMCINotify)
[cpp]  view plain copy
  1. LRESULT CMyDlg::OnMCINotify(WPARAM wParam, LPARAM lParam)  
  2. {  
  3.     if (wParam == MCI_NOTIFY_SUCCESSFUL )  
  4.     {  
  5.         PlayBackMusic();  
  6.     }  
  7.     return 0;  
  8. }  
  9.   
  10.   
  11. void CMyDlg::OpenMciDeveci(void)  
  12. {  
  13.     DWORD dwResult = 0;  
  14.   
  15.     dwResult = m_mciMusic.openDevice();  
  16.     if(0 != dwResult)  
  17.     {  
  18.         MessageBox(m_mciMusic.getErrorMsg(dwResult));  
  19.     }  
  20.   
  21. }  
  22.   
  23.   
  24. void CMyLLKDlg::PlayBackMusic(void)  
  25. {  
  26.   
  27.      m_mciMusic.seekToStart();  
  28.     //m_mciMusic.stop();  
  29.     DWORD dwResult = m_mciMusic.play(this, _T(“your_music.mp3”));  
  30.     if (dwResult != 0)  
  31.     {  
  32.         MessageBox(m_mciMusic.getErrorMsg(dwResult));  
  33.     }  
  34.   
  35. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值