Three MP3 playback Methods
1. Use the Windows Media Player Control
First, insert the Windows Media Player control and drag it to the dialog box. And name the control m_wmpplayer.
Void cmusicdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
Cstring filename;
Cfiledialog DLG (true );
If (DLG. domodal () = idok)
{
Filename = DLG. getpathname ();
M_wmpplayer.seturl (filename );
}
}
2. Use MCI
Include header file # include <VFW. h>, library file vfw32.lib
Hwnd m_audio;
M_audio = mciwndcreate (this-> getsafehwnd (), AfxGetInstanceHandle (), ws_child | ws_visible | mciwndf_nomenu, null );
Void cmusicdlg: onbutton2 ()
{
// Todo: add your control notification handler code here
Cstring filename;
Cfiledialog DLG (true );
If (DLG. domodal () = idok)
{
Filename = DLG. getpathname ();
Mciwndopen (m_audio, filename. getbuffer (0), 0 );
Filename. releasebuffer ();
Mciwndplay (m_audio );
}
}
3. Use DirectShow
DirectX needs to be installed
Include header file # include <dshow. h>, library file strmiids. Lib
Igraphbuilder * pgbuilder;
Imediacontrol * pmcontrol;
Imediaposition * pmpos;
coinitialize (null); // initialize com
// create each object
cocreateinstance (clsid_filtergraph, null, clsctx_inproc, iid_igraphbuilder, (void **) & pgbuilder);
pgbuilder-> QueryInterface (iid_imediacontrol, (void **) & pmcontrol);
pgbuilder-> QueryInterface (iid_imediaposition, (void **) & pmpos);
void cmusicdlg: onbutton3 ()
{< br> // todo: add your control notification handler code here
cstring filename;
cfiledialog DLG (true);
If (DLG. domodal () = idok)
{< br> filename = DLG. getpathname ();
wchar wstrsoundpath [max_path]; // stores the path in the unicode format
multibytetowidechar (cp_acp, 0, filename. getbuffer (0),-1, wstrsoundpath, max_path);
filename. releasebuffer ();
Pgbuilder-> renderfile (wstrsoundpath, null); // call the file
// Play MP3 files
Pmpos-> put_currentposition (0); // move to the file header
Pmcontrol-> Run (); // play
}
}
# Define safe_release (p) if (null! = P) {P-> release (); P = NULL ;}
bool cmusicdlg: destroywindow ()
{< br> // todo: add your specialized code here and/or call the base class
// release the object
safe_release (pmcontrol);
safe_release (pmpos );
safe_release (pgbuilder);
couninitialize (); // release com
return cdialog: destroywindow ();
}