VC ++ notes-Win 32 Multimedia Service

Source: Internet
Author: User
Tags change settings

Windows 95/NT provides rich multimedia service functions, including a large number of low-level to advanced multimedia API functions. With these powerful APIs, you can write multimedia applications at different levels. You can write a book about multimedia services. This section briefly introduces some of the most commonly used multimedia services.

When developing multimedia applications using Visual C ++, you must include mmsystem in all source programs that require multimedia functions. h header file, and the file location should be in windows. h header file. In addition, winmm is used to connect the program. library import, so you should add winmm to the object/library modules column on the Link Page of the Project Settings dialog box. lib, or add the following line to the source program:

# Pragma comment (Lib, "winmm. lib ")

11.6.1
Advanced Audio Functions

Windows provides three Advanced Audio functions for Playing Sound: messagebeep, playsound, and sndplaysound. These three functions can meet the general needs of Playing Waveform sound, but the size of the wave files (waveform sound files) They play cannot exceed kb. If you want to play a large wave file, you should use the MCI service.

Messagebeep has been used by readers. This function is mainly used to play the system alarm sound. The system alarm sound is defined by the user's sounds program in the control panel, or is specified in the [sounds] segment of win. ini. The declaration of this function is:

Bool messagebeep (uint utype );

The utype parameter indicates the alert level, as shown in table 11.4. If the call succeeds, the function returns true.

 

Table 11.4 system alert level

Level

Description

-1

Beep from the Speaker of the machine.

Mb_iconasterisk

Play the sound defined by systemasterisk.

Mb_iconexclamation

Play a sound defined by systemexclamation.

Mb_iconhand

Play a sound defined by systemhand.

Mb_iconquestion

Play a sound defined by systemquestion.

Mb_ OK

Play a sound defined by systemdefault

After playing the video, the messagebeep function returns immediately. If this function cannot play the specified alarm sound, it will play the system default sound defined by systemdefault. If the system default sound cannot be played, then it will beep on the computer's speaker. The above mb_series sounds are not defined.

Messagebeep can only be used to play a few defined sounds. If the program needs to play a digital audio file (*. Wav file) or audio resources, it needs to use the playsound or sndplaysound function.

The playsound function is declared as follows:

Bool playsound (lpcstr pszsound, hmodule hmod, DWORD fdwsound );

 

The parameter pszsound is a string that specifies the sound to be played. This parameter can be the name of the wave file, the name of the wav resource, the pointer to the sound data in the memory, or win in the system registry. system Event sound defined in ini. If this parameter is null, the playing sound is stopped. The hmod parameter is the application instance handle. this parameter is used when playing wav resources; otherwise, it must be null. Fdwsound is a combination of flags, as shown in Table 11.5. If the call succeeds, the function returns true; otherwise, false.

 

Table 11.5 playback flag

Flag

Description

Snd_application

Play the sound using the association specified by the application.

Snd_alias

The pszsound parameter specifies the alias of the system event in the registry or win. ini.

Snd_alias_id

The pszsound parameter specifies a predefined sound identifier.

Snd_async

Play the sound asynchronously. The playsound function returns the result immediately after the playing starts.

Snd_filename

The pszsound parameter specifies the wave file name.

Snd_loop

The replay sound must be used with the snd_async flag.

Snd_memory

Play the sound loaded into the memory. In this case, pszsound is a pointer to the sound data.

Snd_nodefault

Do not play the default sound. If this flag is not displayed, playsound will play the default sound when no sound is found.

Snd_nostop

Playsound does not interrupt the original sound broadcast and returns false immediately.

Snd_nowait

If the driver is busy, the function does not play the sound and returns immediately.

Snd_purge

Stop all sounds related to the call task. If the parameter pszsound is null, all sound is stopped. Otherwise, the sound specified by pszsound is stopped.

Snd_resource

The pszsound parameter is the identifier of the wave resource, and the hmod parameter is used.

Snd_sync

The playsound function returns the synchronous playback sound after playback.

 

 

There is a sound file named the Microsoft sound.wav in the c: \ windows \ Media Directory, which is played when Windows 95 is started. Below we use three methods to call the playsound function to broadcast the Startup Sound of Windows 95.

The first method is to directly broadcast the sound file, and the corresponding code is:

Playsound ("C :\\ Win95 \ media \ The Microsoft sound.wav", null, snd_filename | snd_async );

Note that the path in the parameter uses two consecutive backslash escape to represent a backslash.

The second method is to add the sound file to the resource and then play the sound from the resource. Visual c ++ supports wave resources. You can right-click the resource view and select the import command. Then, in the file selection dialog box, select the Microsoft sound.wav file, the file will be added to the wave resource. Assuming that the sound resource ID is idr_startwin, the following call also outputs the Startup sound:

Playsound (lpctstr) idr_startwin, AfxGetInstanceHandle (), snd_resource | snd_async );

The third method is to use playsound to play the system sound. The Windows Startup Sound is defined by systemstart. Therefore, you can use the following method to play the Startup sound:

Playsound ("systemstart", null, snd_alias | snd_async );

The function sndplaysound is similar to playsound, but one parameter is missing. Function declaration:

Bool sndplaysound (lpcstr lpszsound, uint fusound );

 

Except that the resource name cannot be specified, the parameter lpszsound and playsound are the same. The fusound parameter indicates how to play a sound. It can be a combination of snd_async, snd_loop, snd_memory, snd_nodefault, snd_nostop, and snd_sync. These marks have the same meanings as playsound.

We can see that sndplaysound cannot directly play audio resources. To use this function to play the wave file, you can call the following method:

Sndplaysound ("mysound. wav", snd_async );

11.6.2 MCI

Media control interface (MCI) provides a high level of control over media device interfaces for Windows programs. The program can control media devices such as CD, video recorder, waveform audio device, video play device, and MIDI device without worrying about specific devices. For programmers, they can think of MCI as a row of buttons on the device panel. By selecting different buttons (sending different MCI commands), the device can complete various functions, you do not have to worry about the internal implementation of the device. For example, for play, the video and CD machines have different responses (one is playing a video and the other is playing audio), but for users, they only need to press the same button.

The application controls the media device by sending commands to the MCI. The MCI command interface can be divided into command strings and command messages, which have the same functions. The command string is easy to use, but its execution efficiency is not as efficient as the command message.

All MCI command strings are passed to MCI through the multimedia API function mcisendstring. The declaration of this function is:

Mcierror mcisendstring (

Lpctstr lpszcommand, // MCI command string

Lptstr lpszreturnstring, // buffer for storing feedback

Uint cchreturn, // buffer Length

Handle hwndcallback // handle of the callback window, usually null

); // If successful, 0 is returned; otherwise, an error code is returned.

 

The error code returned by this function can be analyzed using the mcigeterrorstring function. The declaration of this function is:

Bool mcigeterrorstring (

DWORD fdwerror, // error code returned by mcisendstring or mcisendcommand

Lptstr lpszerrortext, // receives the buffer for strings with incorrect descriptions

Uint ccherrortext // buffer Length

);

The following is a simple example of using the mcisendstring function:

Char Buf [50];

Mcierror;

Mcierror = mcisendstring ("open cdaudio", Buf, strlen (BUF), null );

If (mcierror)

{

Mcigeterrorstring (mcierror, Buf, strlen (BUF ));

Afxmessagebox (BUF );

Return;

}

Run the open cdaudio command to open the CD player. If an error occurs (such as no cd in the drive), the error code is returned. You can use the mcigeterrorstring function to obtain the error message string. Open is the command to enable the device for the MCI, and cdaudio is the name of the MCI device. The device types of MCI are listed in Table 11.6.

 

Table 11.6 MCI device types

Device Type

Description

Animation

Animation Devices

Cdaudio

CD player

Dat

Digital audio tape drive

Digitalvideo

Digital video in a window (not based on GDI)

Other

Undefined MCI Device

Overlay

Overlapping devices (simulated videos in Windows)

Bytes

Image scanner

Sequencer

MIDI sequencer

Videodisc

Video Disc Machine

Waveaudio

Audio devices that play digital waveform files

Please note that the device type and device name are different concepts. A device type is a type of MCI device that responds to a group of Common commands, while a device name is the name of an MCI device. The system uses different device names to distinguish different devices of the same device type.

The device name is defined in the [MCI] Section of the Registry or system. ini. The typical [MCI] section is as follows:

[MCI]

Cdaudio = mcicda. DRV

Sequencer = mciseq. DRV

Waveaudio = mciwave. DRV

Avivideo = mciavi. DRV

Videodisc = mcipionr. DRV

The device name is on the left of the equal sign, and the corresponding MCI driver is on the right. When a new MCI driver is installed, the system uses different device names. The device name is usually the same as the device type name in the driver, such as cdaudio and waveaudio, but there are also exceptions. For example, avivideo is a digitalvideo device.

The use of an MCI device generally involves three steps: Enable, use, and disable. Most of the MCI commands can control different media devices. For example, you can use the play command to play a wave file, video file, or CD. Table 11.7 lists commonly used MCI command strings. Most of the commands in the table are universal. Generally, a device name is followed by the MCI command to specify the operation object.

 

Table 11.7 common MCI commands

Command

Description

Capacility

Query device capabilities

Close

Disable the device

Info

Query device information

Open

Enable the device

Pause

Pause the playing or recording of a device

Play

Start device playback

Record

Start

Resume

Resume the pause or recording of a device.

Seek

Change the current position of the media

Set

Change settings

Status

Query device status information

Stop

Stop playing or recording a device

 

 

For example, after a CD player is enabled in the above example, you can send Common commands to control the cdnserver:

Play cdaudio from <location> to <location>. If the from clause is omitted, the playing starts from the current track. If the to clause is omitted, the playing ends.

Pause cdaudio. Pause playback.

Stop cdaudio. Stop playing.

Resume cdaudio. Resume paused playback.

Status cdaudio number of tracks. Query the number of tracks of a CD. Status cdaudio current track can query the current track.

Seek cdaudio to <location>. Move to the specified track.

Set cdaudio door open/closed. Pop up or indent the CD disk.

Close cdaudio. Disable the device.

 

MCI devices can be classified by simple devices and composite devices. Devices such as cdaudio do not use files, which are called simple devices. However, compound devices use data files, such as digitalvideo and waveaudio, during playback, we call these data files a device element.

Specify the device name and device element when opening a compound device. For example, run the following command to open a waveform audio device:

Open mysound.wav type waveaudio

You can only specify the device element for the compound device. For example:

Open mysound.wav

As shown in the following figure, the system can find the registry or [MCI extensions] of win. ini to determine which device to open.

[MCI extensions]

Mid = sequencer

RMI = sequencer

Wav = waveaudio

Avi = avivideo

Sometimes, the program needs to open the same device multiple times to play different data files. For example, no one can deny the possibility of playing two AVI files at the same time on the screen. In this case, a different alias needs to be set for each device that opens, in this way, only two playback devices can be distinguished. For example, the following code opens and plays two AVI files:

Char Buf [50];

Mcisendstring ("Open dillo. Avi type avivideo alias dillo", Buf, strlen (BUF), null );

Mcisendstring ("play dillo repeat", Buf, strlen (BUF), null); // Replay

Mcisendstring ("open search. Avi type avivideo alias search", Buf, strlen (BUF), null );

Mcisendstring ("play search", Buf, strlen (BUF), null );

When you use the open command to open a device, if an alias is specified, aliases will be used for subsequent operations on the device.

So far, we have used the MCI command string. Readers may already have this experience. The command string has the advantage of being easy to learn, but this interface is far from the C/C ++ style. If the program needs to query and set a large amount of data, it is inconvenient to use strings.

The command message interface of MCI provides the C language interface, which is faster and better suited to the needs of C/C ++ programmers. All MCI command messages are sent through the mcisendcommand function. The declaration of this function is:

Mcierror mcisendcommand (

Mcideviceid iddevice, // device ID. this parameter is not required when the device is enabled.

Uint umsg, // Command Message

DWORD fdwcommand, // the flag of the Command Message

DWORD dwparam // point to the structure containing Command Message Parameters

); // If successful, 0 is returned; otherwise, the error code is returned.

 

The code in listing 11.8 demonstrates how to use the MCI command message to open and replay an AVI file:

 

Configuration 11.8

Mci_dgv_open_parms mciopen;

Uint wdeviceid;

Mcierror;

 

Mciopen. lpstrdevicetype = "avivideo"; // device name

Mciopen. lpstrelementname = "dillo. Avi"; // device Element

 

Mcierror = mcisendcommand (0, mci_open,

Mci_open_type | mci_open_element, // The device element is used.

(DWORD) & mciopen );

If (mcierror)

{

Char s [80];

Mcigeterrorstring (mcierror, s, 80 );

Afxmessagebox (s );

Return;

}

Wdeviceid = mciopen. wdeviceid; // Save the device ID

Mci_dgv_play_parms mciplay;

Mcierror = mcisendcommand (wdeviceid, mci_play, mci_dgv_play_repeat,

(DWORD) & mciplay );

...

It can be seen that the use of command messages is much more complex than the use of command strings. Command messages correspond to command strings. For example, open and mci_open provide the same functions. The variable wdeviceid is used to save the device ID. The system uses the ID to identify different devices to ensure that the command is sent to the correct object.

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.