Source text: http://blog.csdn.net/dancewyr/article/details/7183572
MCI plays MP3 audio file routines
The source file needs to include the header file Mmsystem.h, and the library Winmm.lib is added to the Project->settings->link->object/libray module. Or add code #pragma comment (lib, "Winmm.lib")
Mci_open_parms op;
void Cmcidlg::onplay ()
{
Todo:add your control notification handler code here
DWORD cdlen;//Audio file length
Op.dwcallback=null;
Op.lpstralias=null;
op.lpstrdevicetype=_t ("Mpegaudio"); Device type, most files can be set like this
op.lpstrelementname=_t ("D:\\2.mp3"); File path
Op.wdeviceid=null; Save this device number after you open the device successfully
UINT rs; Accept function return result
Send command to open the device, successfully return 0, otherwise return the error number, the third parameter must be mci_open_element
Rs=mcisendcommand (Null,mci_open,mci_open_element, (DWORD) &op);
Mcisendcommand (Null,mci_set,mci_set_door_open,null);
Cdlen=getinfo (op.wdeviceid,mci_status_length);//Get audio file length
if (rs==0)//device is turned on successfully to play the file
{
Mci_play_parms pp;
Pp.dwcallback=null;
pp.dwfrom=0; Where to start playing
Mcisendcommand (Op.wdeviceid,mci_play,mci_notify, (DWORD) &pp);
Play the file, if the third parameter is set to Mci_wait, the program window will be blocked, in order to avoid this can be set to Mci_notify
}
Sleep (Cdlen);//wait according to file length, same as mci_wait effect, Cdlen parameter can control playback time
OnStop (); Close Audio file
}
DWORD cmcidlg::getinfo (UINT wdeviceid,dword Item)
{
Mci_status_parms mcistatusparms;
mcistatusparms.dwcallback= (DWORD) GetSafeHwnd ();
Mcistatusparms.dwitem=item;
mcistatusparms.dwreturn=0;
Mcisendcommand (Wdeviceid,mci_status,mci_status_item, (DWORD) &mcistatusparms);
return mcistatusparms.dwreturn;
}
void Cmcidlg::onstop ()
{
Todo:add your control notification handler code here
Send Mci_close command to shut down device during WM_CLOSE message processing
Mci_generic_parms GP;
Gp.dwcallback=null;
Mcisendcommand (op.wdeviceid,mci_close,mci_wait, (DWORD) &GP);
}
==============================================================================================
Joining music is the simplest of all the ways to enhance your application's functionality. Almost every computer game or multimedia program has a background in some kind of midi or CD music. Music can make users happy, and playing the right music in a suitable situation can make the programmer and his VC + + program glow.
The first part of MIDI playback
Musical Instrument Digital Interface (MIDI) is an agreement developed by some of the world's largest companies, including companies that produce electronic music synthesizers, and is later adopted by the computer industry as a standard format for multimedia music files. MIDI files are generally small and require high requirements for hardware devices.
First, the principle
Although Microsoft supports MIDI files, Visual C + + or MFC does not create any components to implement this support, but the Microsoft API provides three different ways to achieve MIDI playback:
MCI (The Media Control Interface). This is the most basic approach and this article will discuss this approach in detail.
Stream buffers. This format allows applications to allocate buffers for MIDI data. Stream buffers are useful when you need precise control over MIDI playback.
Low-level MIDI device. Applications that require full control of MIDI data can use this approach.
MCI can be done with Mcisendcommand () and mciSendString (), and this article uses only the Mcisendcommand () function.
Prototype: DWORD Mcisendcommand (UINT wdeviceid,uint wmessage,dword dwparam1,dword dwParam2);
Parameter: Wdeviceid: The device ID that accepts the message
WMESSAGE:MCI Command Message
DWPARAM1: Flag Bit of command
DWPARAM2: Pointer to the parameter block used
Return value: The call succeeds, returns 0; otherwise, the low word in the return double word holds an error message.
Playback control of two MIDI
1. Turn on the device
Mci_open_parms openparms;
Openparms.lpstrdevicetype =
(LPCSTR) Mci_devtype_sequencer;//midi type
Openparms.lpstrelementname = (LPCSTR) Filename;
Openparms.wdeviceid = 0;
Mcisendcommand (NULL, Mci_open,
mci_wait | Mci_open_type |
mci_open_type_id | Mci_open_element,
(DWORD) (LPVOID) &openparms)
The MCI device ID indicates which device is open, and when the Mci_open command is sent, this value is returned in the parameter block-should be saved for backup.
2. Turn off the device
Mcisendcommand (M_wdeviceid, mci_close, NULL, NULL);
3. Play
Mci_play_parms playparms;
Playparms.dwfrom = 0;
Specify where (time) to play from
Mcisendcommand (M_wdeviceid, Mci_play,
Mci_from, (DWORD) (LPVOID)
&playparms));
4. Time out
Mci_play_parms playparms;
Mcisendcommand (M_wdeviceid, Mci_pause, 0,
(DWORD) (LPVOID) &playparms);
5. Stop it
Mcisendcommand (M_wdeviceid, mci_stop, NULL, NULL);
6. Jump
* Jump to any place
Mci_seek_parms seekparms;
SEEKPARMS.DWTO = (Nminute * + nsecond) * 1000;
The target time of the jump, the time unit is milliseconds
Mcisendcommand (M_wdeviceid, Mci_seek, mci_to
| Mci_wait, (DWORD) (LPVOID)
&seekparms);
* Skip to file header
Mcisendcommand (M_wdeviceid, Mci_seek,
Mci_seek_to_start, NULL);
* Skip to end of file
Mcisendcommand (M_wdeviceid, Mci_seek,
Mci_seek_to_end, NULL);
7. Querying current information
Mci_status_parms statusparms;
Statusparms.dwitem = Mci_seq_status_divtype;
Mcisendcommand (M_wdeviceid, MCi_STATUS,
mci_wait | Mci_status_item,
(DWORD) (LPVOID) &statusparms);
The return information is stored in the Statusparms.dwreturn.
MCi_STATUS logo
Mci_status_length Get file length
Mci_status_mode to get the current status of the file playback
Mci_status_position get the current location of the file playback
Mci_status_time_format get the current time format
Mci_seq_status_divtype to determine whether a file is a ppqn type or a SMPTE type
Mci_seq_status_tempo gets the current playback speed, PQRN type,
This value is a beat/min, SMPTE type, this value is S/sec
8. Set time format and playback speed
Mci_set_parms setparms;
Setparms.dwtimeformat = Mci_format_milliseconds;
Set the time unit to milliseconds
Mcisendcommand (M_wdeviceid,
Mci_set, Mci_set_time_format,
(DWORD) (LPVOID) &setparms);
Mci_seq_set_tempo Set playback speed,
PQRN type, this value is beat/min,
SMPTE type, this value is S/sec
The second part of the WAV file playback
First, the principle
The MicroSoft API provides three different ways to implement WAV playback:
The PlaySound () function. It can play the wave format sound by single-line encoding. This function has two restrictions: the sound data must be fully loaded into physical memory, and the data format must be supported by one of the configured audio drives. Based on experience, PlaySound () applies to files below 100K.
MCI (The Media Control Interface), similar to playing MIDI files in the previous chapter, can play more than 100K of files.
Low-level wave audio devices. Use these devices to run application files that completely control wave data.
Second, WAV file playback control
Because MCI is also used, the same as in the previous chapter, only the different parts are listed.
1. Turn on the device
Change MIDI's mci_devtype_sequencer to "Waveaudio"
2. Recording
Mci_record_parms recordparms;
Mcisendcommand (M_wdeviceid, Mci_record,
NULL, (DWORD) (LPVOID)
&recordparms);
3. Save Recording
Mci_save_parms saveparms;
Saveparms.lpfilename = (LPCSTR) Filename;
Mcisendcommand (M_wdeviceid, Mci_save,
Mci_save_file | Mci_wait,
(DWORD) (LPVOID) &saveparms);
The third part of the CD playback
The unique advantage of CDs is that it is designed by composers and produced by music manufacturers. When different computers play MIDI files, the sound effects are not the same, but the sound effects of the CDs are always the same. You will be amazed at the effects of high-quality audio on computer users. We still use MCI to play the CD, most of the playback controls are the same as the first two, and only the different parts are listed.
1. Door Drive
Mcisendcommand (M_wdeviceid, Mci_set,
Mci_set_door_open, NULL);
2. Guan Drive Door
Mcisendcommand (M_wdeviceid, Mci_set,
mci_set_door_closed, NULL);
3. Turn on the device
Change the MIDI mci_devtype_sequencer to Mci_devtype_cd_audio
4. Play
Specifies that the playback start point must be transformed by MCI_MAKE_TMSF (Track,minute,second,frame)
5. Querying current information
Mci_status_current_track Get current track
Mci_status_length get CD or specify track length
Mci_status_mode get the current state of the drive
Mci_status_number_of_tracks get the number of CD tracks
Mci_status_position to get the position under the current format
Mci_status_ready Check if the device is ready
Mci_status_time_format get the current time format
Mci_status_media_present Check to make sure the CD is inside the drive
Mci_cda_status_type_track Check that a track is an audio track
Attention:
Use the Mci_status_length parameter to query the CD and track length, return value by calling Mci_msf_minute (), Mci_msf_second () to minutes, seconds.
The Mci_status_position parameter return value calls Mci_tmsf_track (), Mci_tmsf_minute (), Mci_tmsf_second (), mci_tmsf_frame to get the current position of the channel, minute, second, frame.
6. Jump
The goal of the jump must be MCI_MAKE_TMSF (track,minute,second,frame) conversion It is best to separate the above three formats, or make a dynamic connection library. Add modules in project-->setting-->link-->object/library winmm.lib, which is included in the source program.
MCI calls are simple, powerful, and can meet the basic needs of everyday multimedia programming. However, MCI can only play one file at a time, using DirectSound technology to achieve simultaneous playback of more than eight WAV files.
==============================================================================================
Using the MCI API, the source file needs to include the header file Mmsystem.h, and the library Winmm.lib is added to the Project->settings->link->object/libray module.
1. Introduction to MCI
MCI (Media Control Interface) provides Windows programs with the ability to control the interface of media devices at a high level. The program does not have to care about specific devices, it can control the laser player (CD), video disc, waveform audio devices, videos playback devices and MIDI devices and other media devices.
For programmers, MCI can be understood as a row of buttons on the device panel, which allows the device to perform various functions by selecting different keys (sending different MCI commands) without having to worry about the internal implementation of the device.
For example, for play, the video disc machine and the CD player have different reactions (one is to play videos, one plays audio), but to the user only need to press the same button.
The application controls the media device by sending commands to MCI. The MCI command interface has the same functionality as both the command string and the command message. The command string is simple to use, but its execution is less efficient than the command message.
All MCI command strings are passed to MCI via the Multimedia API function mciSendString, which is declared as:
Mcierror mciSendString (
LPCTSTR lpszcommand,//mci command string
LPTSTR lpszreturnstring,//buffer for storing feedback information
UINT Cchreturn,//length of buffer
HANDLE the handle to the Hwndcallback//callback window, typically null
); Returns 0 if successful, otherwise the error code is returned.
The error code returned by the function can be parsed with the mcigeterrorstring function, which is declared as:
BOOL mcigeterrorstring (
DWORD fdwerror,//function mcisendstring return error code
LPTSTR Lpszerrortext,//buffer that receives a string that describes the error
UINT Ccherrortext//Length of buffer
);
Here is a simple example of using the mciSendString function:
Char buf[50];
Mcierror Mcierror;
Mcierror=mcisendstring ("Open CDAudio", Buf,strlen (BUF), NULL);
if (mcierror)
{
Mcigeterrorstring (Mcierror,buf,strlen (BUF));
AfxMessageBox (BUF);
Return
}
The Open CDAudio command opens the CD player and returns an error code if an error occurs (such as no CD in the drive), at which point the error message string can be obtained with the mcigeterrorstring function.
2. MCI Equipment
Open is the MCI command that opens the device, and CDAudio is the MCI device name. The device types of MCI are as follows:
Animation Animation Equipment
CDAudio CD Player
DAT digital Audio tape drive
Digitalvideo digital Video in a window (not GDI-based)
MCI devices not defined by other
Overlay overlapping devices (analog video in Windows)
Scanner Imaging scanner
Sequencer MIDI Sequencer
Videodisc Video Disc Machine
Waveaudio Audio devices that play digital waveform files
The device name is defined in the [MCI] section of the registry or System.ini, and the typical [MCI] segment is as follows:
[MCI]
Cdaudio=mcicda.drv
Sequencer=mciseq.drv
Waveaudio=mciwave.drv
Avivideo=mciavi.drv
Videodisc=mcipionr.drv
The left side of the equals sign is the device name, and the corresponding MCI driver is on the right. When a new MCI driver is installed, the system is differentiated by a different device name.
3. MCI command
Using MCI devices typically involves opening, using, and closing three processes, and the commonly used MCI commands are:
Open device
Close Device
Play Start device playback
Stop to stop a device from playing or recording
Record starts recording
Save saves device content
Pause pauses the playback or recording of a device
Resume resuming a device that pauses playback or recording
Seek to change the current position of the media
capacility Query Equipment Capabilities
Info Query Device Information
Status Query device state information
Most of the MCI commands can control different media devices, but the record and save commands are not available to all MCI devices.
The use of the MCI command is arbitrary, as long as it is opened first, closed at last, and can be arbitrarily invoked in the middle.
(1) Open device
MCI equipment must be opened before use, of course, after use must be closed, so as not to affect the use of others.
Open Device_name type Device_type alias Device_alias
Device_name the name of the device to use, usually the file name.
Type Device_type device types, such as Waveaudio or sequencer, can be omitted.
Alias Device_alias device alias, specified to replace the device name in other commands.
(2) play start device playback
When the MCI device is turned on, it can be played, using the device name or alias.
Play Device_alias from pos1 to Pos2 wait repeat
If the from is omitted from the current track starts playing, if omitted to play to the end.
If you specify wait, wait until the command is finished.
If the repeat is indicated, it will be played repeatedly.
If both wait and repeat are indicated, the command does not return, and this thread is blocked, which usually causes the program to become unresponsive.
(3) Play CD
void Ctttview::onopencd ()
{
mciSendString ("Open CDAudio", null,0,null);
mciSendString ("Play CDAudio", null,0,null);
);
void Ctttview::onstopcd ()
{
mciSendString ("Stop CDAudio", null,0,null);
mciSendString ("Close CDAudio", null,0,null);
);
It's OK:
Pause CDAudio pauses playback.
Resume CDAudio continues to be paused for playback.
Seek CDAudio to < position > move to the specified track.
Set CDAudio door open/closed eject or indent the CD disc.
(4) Play multimedia files
void Ctttview::onmymenu ()
{
mciSendString ("Open myfolder\\tada.wav alias AA", Null,0,null);
Or mciSendString ("Open Myfolder\\flourish.mid alias AA", Null,0,null);
Or mciSendString ("Open Myfolder\\clock.avi alias AA", Null,0,null);
mciSendString ("Play AA Wait", null,0,null);
mciSendString ("Close AA", Null,0,m_hwnd);
);
(5) Recording sound
void Ctttview::onstartrecord ()
{
mciSendString ("Open new type Waveaudio alias AA", Null,0,null);
mciSendString ("Record AA", null,0,null);
);
void Ctttview::onstoprecord ()
{
mciSendString ("Save AA C:\\aaa.wav Wait", null,0,null);
mciSendString ("Close AA", null,0,null);
);
4. MCI command Message
So far, we have used the MCI command string. It can be found that the command string has the advantage of being easy to learn, but this interface is far from the style of C + +, and if the program wants to query and set up a lot of data, it will be inconvenient to use the string form.
MCI's command-message interface provides a C-language interface that is faster and more responsive to the needs of C + + programmers.
All MCI command messages are sent through the Mcisendcommand function, and the function declaration is:
Mcierror Mcisendcommand (
Mcideviceid Widdevice,//ID of the device, without this parameter when opening the device
UINT umsg,//Command message
DWORD Fdwcommand,//Flag for command message
DWORD Dwparam//points to the structure that contains the command message arguments
); Returns 0 if successful, otherwise the error code is returned
The command message umsg corresponds to the command string, for example, Open has the same functionality as Mci_open.
The variable Wdeviceid is used to hold the ID of the device, and the system uses the ID to identify different devices to ensure that the command is sent to the correct object.
void Ctttview::onmymenu ()
{
Mci_open_parms Mciopen;
UINT Wdeviceid;
Mciopen.lpstrdevicetype = "AVIVideo";
Mciopen.lpstrelementname = "Myfolder\\clock.avi";
Mcisendcommand (0, Mci_open, Mci_open_element, (DWORD) &mciopen);
Wdeviceid=mciopen.wdeviceid;
Mci_play_parms Mciplay;
Mcisendcommand (Wdeviceid, Mci_play, mci_wait, (DWORD) &mciplay);
);
As you can see, command messages are much more complex than command strings, but they are more efficient to execute.
VC + + in MCI Play audio file "turn"