Audio file playback in the Windows environment

Source: Internet
Author: User

recently a small project to use the music playback module, so turned over the information, found that the Windows environment plays audio files haveMCI, DirectShow, etc.several ways, of course, can also be played using the underlying API Waveout. The first method is the simplest, the program only need to send the MCI command to control the media device, audio file playback, pause, stop and other operations. The other two ways is more cumbersome, first build DirectShow development environment is more difficult, and use waveout play audio, you need to decode the audio to obtain the PCM data before playing. For simple usage scenarios that do not involve audio data processing and only play audio, it is recommended that you play the audio file in MCI mode. The use of MCI can be summarized as follows: First obtain the device ID, and then send a variety of media control commands to the device to achieve media playback operations. The following is a simple package of MCI using the interface, header, source files are as follows:
 #ifndef _ Music_playing_manager_h#define _music_playing_manager_h#include <MMSystem.h> #pragma comment (lib, "Winmm.lib" ) #define Result_error-999class Musicplayingmanager{public:musicplayingmanager (DWORD dwcallback); ~ Musicplayingmanager (void);p Rivate:dword m_dwcallback; Mcideviceid M_mcideviceid; WCHAR M_szfilename[max_path];p ublic://Open the music file void Openmusicfile ();//play/pause/stop music void Playmusic (); void Pausemusic (); void Stopmusic ();//Turn off audio device void CloseDevice ();}; #endif 
#include "StdAfx.h" #include "MusicPlayingManager.h" Musicplayingmanager::musicplayingmanager (DWORD dwcallback) {m_ Mcideviceid = Result_error;m_dwcallback = Dwcallback;} Musicplayingmanager::~musicplayingmanager (void) {CloseDevice ();} Turn off device void Musicplayingmanager::closedevice () {if (M_mcideviceid! = result_error) {Mcisendcommand (M_mcideviceid, MCI_ CLOSE, NULL, NULL); M_mcideviceid = Result_error;}} Open music file void Musicplayingmanager::openmusicfile () {//Select file to play CFileDialog Dlg (TRUE, NULL, Null,ofn_filemustexist | Ofn_pathmustexist,l "Music (*.mp3) | *.mp3| | ", NULL);d lg.m_ofn.lpstrTitle = L" Please select the song file "; if (dlg. DoModal () = = IDOK) {CString srcfilename = dlg. GetPathName (); wcscpy_s (M_szfilename, Srcfilename.getbuffer (Srcfilename.getlength ())); Srcfilename.releasebuffer ( );} First turn off the device CloseDevice ();//re-open the device mcierror mcierr = ERROR_SUCCESS; Mci_open_parms Mciparams; ZeroMemory (&mciparams, sizeof (Mciparams)); mciparams.lpstrdevicetype = Null;mciparams.lpstrelementname = m_ Szfilename;mciparams.dwcallback = M_dwcallbacK;mcierr = Mcisendcommand (NULL, Mci_open, mci_notify | Mci_open_element, (DWORD_PTR) &mciparams); if (Mcierr = = ERROR_SUCCESS) {M_mcideviceid = Mciparams.wdeviceid; Mci_set_parms Mciparams; ZeroMemory (&mciparams, sizeof (Mciparams)); mciparams.dwcallback = M_dwcallback;mciparams.dwtimeformat = MCI_ Format_milliseconds;mcisendcommand (M_mcideviceid, Mci_set, mci_notify | Mci_set_time_format, (DWORD_PTR) &mciparams);} Else{m_mcideviceid = Result_error; AfxMessageBox (L "Device failed to open");}} Play music void Musicplayingmanager::P laymusic () {if (M_mcideviceid! = result_error) {mci_play_parms mciparams; ZeroMemory (&mciparams, sizeof (Mciparams)); mciparams.dwcallback = M_dwcallback;mcisendcommand (M_mciDeviceId, Mci_play, Mci_notify, (dword_ptr) &mciparams);}} Pause music void Musicplayingmanager::P ausemusic () {if (M_mcideviceid! = result_error) {mci_generic_parms mciparams; ZeroMemory (&mciparams, sizeof (Mciparams)); mciparams.dwcallback = M_dwcallback;mcisendcommand (M_mciDeviceId, Mci_pause, Mci_notify, (DWORD_PTR) &mciparams);}} Stop music void Musicplayingmanager::stopmusic () {if (M_mcideviceid! = result_error) {mci_generic_parms mciparams; ZeroMemory (&mciparams, sizeof (Mciparams)); mciparams.dwcallback = M_dwcallback;mcisendcommand (M_mciDeviceId, Mci_stop, Mci_notify, (dword_ptr) &mciparams) Mcisendcommand (M_mcideviceid, Mci_seek, MCI_SEEK_TO_START, DWORD_ PTR) &mciparams);}}
The usage is simple, as follows:
Open file void Ctestmusicplayingdlg::onbnclickedbuttonopenmusic () {m_pmusicplayingmgr->openmusicfile (); m_ Pmusicplayingmgr->playmusic ();} Play music void Ctestmusicplayingdlg::onbnclickedbuttonplaymusic () {m_pmusicplayingmgr->playmusic ();} Pause music void Ctestmusicplayingdlg::onbnclickedbuttonpausemusic () {m_pmusicplayingmgr->pausemusic ();} Stop music void Ctestmusicplayingdlg::onbnclickedbuttonstopmusic () {m_pmusicplayingmgr->stopmusic ();}
        Complete Project download link: http://download.csdn.net/detail/u013085897/7770721



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.