MFC下MCI的使用播放音乐

时间:2022-04-30 10:56:28

最近研究了一下MFC下的音乐的播放,主要使用了MCI

1.需要包含的库文件

在链接资源里(link)添加库文件VFW32.lib winmm.lib

 

2.包含的头文件

1 #include <mmsystem.h>
2 
3 #include "stdafx.h"
4 
5 #include "vfw.h"
6 
7 #include <digitalv.h>
8 
9 #pragma comment(lib,"winmm.lib")


3.需要调用的成员变量和成员函数的声明、定义

 1 class CMp3Dlg : public CDialog
 2 {
 3 // Construction
 4 public:
 5     CMp3Dlg(CWnd* pParent = NULL);    // standard constructor
 6 
 7 // Dialog Data
 8     //{{AFX_DATA(CMp3Dlg)
 9     enum { IDD = IDD_MP3_DIALOG };
10     CButton    m_Play;
11     CString    m_Path;
12     //}}AFX_DATA
13 
14     // ClassWizard generated virtual function overrides
15     //{{AFX_VIRTUAL(CMp3Dlg)
16     protected:
17     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
18     //}}AFX_VIRTUAL
19 
20 // Implementation
21 protected:
22     HICON m_hIcon;
23 
24     // Generated message map functions
25     //{{AFX_MSG(CMp3Dlg)
26     virtual BOOL OnInitDialog();
27     afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
28     afx_msg void OnPaint();
29     afx_msg HCURSOR OnQueryDragIcon();
30     afx_msg void OnButtonOpenFile();
31     afx_msg void OnButtonPlayMusic();
32     afx_msg void OnButtonCyclePlay();
33     afx_msg void OnMciNotify(WPARAM wParam,LPARAM lParam);
34     //}}AFX_MSG
35     DECLARE_MESSAGE_MAP()
36 
37 public:
38     void CyclePlay();
39     
40 
41 private:
42     HWND m_video;
43     BOOL Pause;
44 
45     MCIDEVICEID m_wID;
46 };

4.添加消息响应

 1 BEGIN_MESSAGE_MAP(CMp3Dlg, CDialog)
 2     //{{AFX_MSG_MAP(CMp3Dlg)
 3     ON_WM_SYSCOMMAND()
 4     ON_WM_PAINT()
 5     ON_WM_QUERYDRAGICON()
 6     ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpenFile)
 7     ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlayMusic)
 8     ON_BN_CLICKED(IDC_BUTTON_CYCLE_PLAY, OnButtonCyclePlay)
 9     ON_MESSAGE(MM_MCINOTIFY,OnMciNotify)
10     //}}AFX_MSG_MAP
11 END_MESSAGE_MAP()

5.打开相应的文件
方法一:

 1 void CMp3Dlg::OnButtonOpenFile() 
 2 {
 3     // TODO: Add your control notification handler code here
 4     TCHAR szBuffer[MAX_PATH] = {0};
 5     BROWSEINFO bi;
 6     ZeroMemory(&bi,sizeof(BROWSEINFO));
 7     bi.hwndOwner = NULL;
 8     bi.pszDisplayName = szBuffer;
 9     bi.lpszTitle = _T("从下面选择文件或者文件夹:");
10     bi.ulFlags = BIF_BROWSEINCLUDEFILES;
11     LPITEMIDLIST idl = SHBrowseForFolder(&bi);
12 
13     if (NULL == idl)
14     {
15         return;
16     }
17     SHGetPathFromIDList(idl,szBuffer);
18 }

方法二:

 1 void CMp3Dlg::OnButtonOpenFile()
 2 {
 3     m_video = NULL;
 4     if (m_video == NULL)
 5     {
 6         CFileDialog filedialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,"MP3 Files (*.mp3)|*.mp3|");
 7         if (filedialog.DoModal() == IDOK)
 8         {
 9             m_Path = filedialog.GetPathName();
10             UpdateData(FALSE);
11         }
12     }
13 }

6.音乐的播放
方法一:

 1 void CMp3Dlg::OnButtonPlayMusic() 
 2 {
 3     // TODO: Add your control notification handler code here
 4     m_video = NULL;    
 5     if(m_video == NULL)
 6     {
 7         m_video = MCIWndCreate(this->GetSafeHwnd(),
 8             AfxGetInstanceHandle(),
 9           WS_CHILD | WS_VISIBLE|MCIWNDF_NOMENU,m_Path);
10     }
11     else
12     {
13         MCIWndHome(m_video);
14     }
15     MCIWndPlay(m_video);
16     m_Play.EnableWindow(FALSE);
17 }

方法二:

 1 void CMp3Dlg::OnButtonCyclePlay() 
 2 {
 3     // TODO: Add your control notification handler code here
 4     MCI_OPEN_PARMS mciOP;
 5     DWORD dwReturn;
 6 
 7     
 8     //ASSERT(m_wID == NULL);
 9 
10     mciOP.lpstrDeviceType = NULL;
11     mciOP.lpstrElementName = m_Path;
12     dwReturn = mciSendCommand(0,
13                     MCI_OPEN,
14                     MCI_OPEN_ELEMENT|MCI_WAIT|MCI_OPEN_SHAREABLE,
15                     (DWORD)(LPVOID)&mciOP);
16 
17     if (dwReturn == 0)
18     {
19         m_wID = mciOP.wDeviceID;
20     }
21     else
22     {
23         m_wID = NULL;
24     }
25 
26     CyclePlay();
27 }
28 
29 void CMp3Dlg::CyclePlay()
30 {
31     MCI_PLAY_PARMS PlayParms;
32     PlayParms.dwCallback = (DWORD)GetSafeHwnd();
33     PlayParms.dwFrom = 0;
34     //mciSendCommand(m_wID,MCI_PLAY,MCI_DGV_PLAY_REPEAT,(DWORD)(LPVOID)&PlayParms);
35     mciSendCommand(m_wID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&PlayParms);
36 }

7.循环播放
方法一:

 1 mciSendCommand(m_wID,MCI_PLAY,MCI_DGV_PLAY_REPEAT,(DWORD)(LPVOID)&PlayParms);

 1 //mciSendCommand(m_wID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&PlayParms);  

将上面的35行注释掉,使用34行

方法二:添加消息响应函数

 1 void CMp3Dlg::OnMciNotify(WPARAM wParam,LPARAM lParam)
 2 {
 3         if(MCI_NOTIFY_SUCCESSFUL == wParam)
 4         {
 5             //mciSendCommand(m_wID,MCI_CLOSE,0,NULL);
 6             //CyclePlay();
 7             MCI_PLAY_PARMS PlayParms;            
 8             PlayParms.dwFrom = 0;            
 9             PlayParms.dwCallback = (DWORD)m_hWnd;            
10             mciSendCommand(m_wID, MCI_SEEK,MCI_SEEK_TO_START, NULL);            
11             mciSendCommand(m_wID, MCI_PLAY, MCI_NOTIFY, (DWORD)(LPVOID)&PlayParms);
12         }
13 }

以上所述经过测试!