Use VC to write a simple MIDI/WAV player

Source: Internet
Author: User
Windows mCi (media control interface) Yes Windows Provides high-level and general control over multimedia devices. . It provides a set of device-independent functions and Command to effectively control multimedia devices. By using MCI, we can easily perform operations on multimedia without having to understand the working principle of multimedia devices and call the underlying function library, which greatly reduces the difficulty and burden of development. Here is a small example of a WAV/MIDI player, which can be used in the future.
1. Create a dialog box-based project midiplay and add seven buttons in the dialog box. The buttons are id_open, id_play, id_pause, id_previous, id_next, id_stop, and id_exit, add a slider control.

2. Add member variables to the cmidiplaydlg class:
Cstring strfileext;
DWORD dcurrentposition;
Unsigned long m_dlength;
Bool ispause;
Word m_wdeviceid;
Cstring strfilename;

3. The code of the function corresponding to the message of each button is as follows: void cmidiplaydlg: onopen ()
{
// Todo: add your control notification handler code here
Cfiledialog file (true, "", "", ofn_filemustexist, "(*. Mid and *. wav files) | *. wav | *. Mid ");
If (file. domodal () = idok)
{
Strfilename = file. getfilename ();
Strfileext = file. getfileext ();
}
This-> setwindowtext ("click the play button to enjoy" + strfilename );
}

Void cmidiplaydlg: onplay ()
{
// Todo: add your control notification handler code here
Ispause = true;
Settimer (1,33, null );
Mcisendcommand (m_wdeviceid, mci_close, 0, null );
Mci_open_parms mciopen;
Mci_play_parms mciplay;
Mciopen. lpstrelementname = strfilename. getbuffer (strfilename. getlength ());
Mcisendcommand (null, mci_open, mci_open_element, (DWORD) (lpvoid) & mciopen );
M_wdeviceid = mciopen. wdeviceid;
Mci_status_parms mcistatusparms;
Mcistatusparms. dwitem = mci_status_length;
Mcisendcommand (m_wdeviceid, mci_status, mci_wait | mci_status_item, (DWORD) (lpvoid) & mcistatusparms );
M_dlength = mcistatusparms. dwreturn;
Mcisendcommand (m_wdeviceid, mci_play, 0, (DWORD) (lpvoid) & mciplay );
M_position.setrange (0, m_dlength );
M_position.setpos (0 );
}

Void cmidiplaydlg: onstop ()
{
// Todo: add your control notification handler code here
Mcisendcommand (m_wdeviceid, mci_stop, 0, null );
// M_position.setpos (0 );
}

Void cmidiplaydlg: onpause ()
{
// Todo: add your control notification handler code here
If (ispause)
{
Ispause = false;
Mci_generic_parms mcipause;
Mcisendcommand (m_wdeviceid, mci_pause, 0, (DWORD) (lpvoid) & mcipause );
}
Else
{
Ispause = true;
If (strfileext = "mid" | strfileext = "mid ")
{
Mci_status_parms mcistatusparms;
Mci_play_parms mciplayparms;
Mcistatusparms. dwitem = mci_status_position;
Mcisendcommand (m_wdeviceid, mci_status, mci_status_item, (DWORD) (lpvoid) & mcistatusparms );
}
Else
{
Mci_generic_parms mciresume;
Mcisendcommand (m_wdeviceid, mci_resume, 0, (DWORD) (lpvoid) & mciresume );
}
}


}

Void cmidiplaydlg: onprevious ()
{
// Todo: add your control notification handler code here
Ispause = true;
Mci_status_parms mcistatusparms;
Mci_play_parms mciplayparms;
Mcistatusparms. dwitem = mci_status_position;
Mcisendcommand (m_wdeviceid, mci_status, mci_status_item, (DWORD) (lpvoid) & mcistatusparms );
Dcurrentposition = mcistatusparms. dwreturn;
If (dcurrentposition <= (m_dlength/16 ))
{
Mcisendcommand (m_wdeviceid, mci_seek, mci_seek_to_start, null );
Mcisendcommand (m_wdeviceid, mci_play, 0, (DWORD) (lpvoid) & mciplayparms );
}
Else
{
Mciplayparms. dwfrom = dcurrentposition-(DWORD) (m_dlength/16 );
Mcisendcommand (m_wdeviceid, mci_play, mci_from, (DWORD) (lpvoid) & mciplayparms );
}

}

Void cmidiplaydlg: onnext ()
{
// Todo: add your control notification handler code here
Ispause = true;
Mci_status_parms mcistatusparms;
Mci_play_parms mciplayparms;
Mcistatusparms. dwitem = mci_status_position;
Mcisendcommand (m_wdeviceid, mci_status, mci_status_item, (DWORD) (lpvoid) & mcistatusparms );
Dcurrentposition = mcistatusparms. dwreturn;
If (m_dLength-dCurrentPosition) <= (m_dlength/16 ))
{
Mcisendcommand (m_wdeviceid, mci_seek, mci_seek_to_end, null );
Mcisendcommand (m_wdeviceid, mci_play, 0, (DWORD) (lpvoid) & mciplayparms );
}
Else
{
Mciplayparms. dwfrom = dcurrentposition + (DWORD) (m_dlength/16 );
Mcisendcommand (m_wdeviceid, mci_play, mci_from, (DWORD) (lpvoid) & mciplayparms );
}
}

4. added the timer function void cmidiplaydlg: ontimer (uint nidevent)
{
// Todo: add your message handler code here and/or call default
Mci_status_parms mcistatusparms;
Mcistatusparms. dwitem = mci_status_position;
Mcisendcommand (m_wdeviceid, mci_status, mci_status_item,
(DWORD) (lpvoid) & mcistatusparms );
Dcurrentposition = mcistatusparms. dwreturn;
M_position.setpos (dcurrentposition );

Cdialog: ontimer (nidevent );
}

5. Add it to the stdafx. h file.
# Include <mmsystem. h>
# Pragma comment (Lib, "winmm. lib ")

This is a simple MIDI/WAV player.

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.