Playback of music using MCI in MFC

Source: Internet
Author: User

Recently I have studied playing music in MFC, mainly using MCI.

1. library files to be included

Add the library file vfw32.lib winmm. lib to the link resource.

 

2. Included header files

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. Declaration and definition of member variables and member functions to be called

 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_DATA13 14     // ClassWizard generated virtual function overrides15     //{{AFX_VIRTUAL(CMp3Dlg)16     protected:17     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support18     //}}AFX_VIRTUAL19 20 // Implementation21 protected:22     HICON m_hIcon;23 24     // Generated message map functions25     //{{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_MSG35     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. Add a message response

 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_MAP11 END_MESSAGE_MAP()

5. Open the corresponding file
Method 1:

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 ("select a file or folder from the following:"); 10 bi. ulflags = bif_browseincludefiles; 11 lpitemidlist IDL = shbrowseforfolder (& BI); 12 13 if (null = IDL) 14 {15 return; 16} 17 shgetpathfromidlist (IDL, szbuffer); 18}

Method 2:

 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. Playing music
Method 1:

 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     else12     {13         MCIWndHome(m_video);14     }15     MCIWndPlay(m_video);16     m_Play.EnableWindow(FALSE);17 }

Method 2:

 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     else22     {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. Loop playback
Method 1:

1 mcisendcommand (m_wid, mci_play, mci_dgv_play_repeat, (DWORD) (lpvoid) & playparms );

1 // mcisendcommand (m_wid, mci_play, mci_notify, (DWORD) (lpvoid) & playparms );

Comment out the 35 rows above and use 34 rows

Method 2: Add a message Response Function

 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 }

The above has been tested!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.