Win 32 multimedia service

zhaozj2021-02-16  163

Windows 95 / NT provides a wealth of multimedia service features, including a large number of multimedia API functions from low to advanced. With these powerful APIs, users can write multimedia applications at different levels. The content of the multimedia service can be written at all, this section only briefly introduces some of the most common multimedia services to the reader.

When developing multimedia applications with Visual C , users must include MMSystem.h header in all source programs to use to multimedia functions, and the file location should be behind Windows.h header files. In addition, when the connection program is used to use the WinMM.Lib to introduce the library, the user should add Winmm.lib in the Object / library modules column of the Linked page of the Project Settings dialog, or add the following line in the source program:

#pragma comment (Lib, "Winmm.Lib")

11.6.1 Advanced audio functions

Windows offers a high-grade audio function for three special playback sounds: MessageBeep, Playsound, and SndPlaySound. These three functions can meet the general needs of playing waveform sounds, but they are playing a WAVE file (wave-like sound file) size cannot exceed 100KB, and if you want to play a large WAVE file, you should use the MCI service.

MessageBeep readers have been used, which is mainly used to play the system alarm sound. The system alarm sound is defined by the user in the sounds program in the control panel, or specifies in the [Sounds] segment of Win.ini. The declaration of this function is:

BOOL messagebeep (uint utype);

The parameter utype illustrates the alarm stage, as shown in Table 11.4. If successful, the function returns True.

Table 11.4 System Alarm Level

Level Description-1 Beeps from the speaker of the machine. MB_ICONASTERISK played the sound defined by SystemAnsterisk. MB_ICONEXCLAMATION Plays the sound defined by systemExclamation. MB_ICONHAND plays the sound defined by SystemHand. MB_ICONQUESTION played by the sound defined by systemquestion. MB_OK played by SYSTEMDEFAULT

After starting playback, the MessageBeep function returns immediately. If the function cannot play the specified alarm sound, it plays the system default sound default system defined by SystemDefault. If the system default sound can't play, then it will be beep on the computer's speaker. The MB_ series sound of the table on the default is 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, you need to use the Playsound or SndPlaySound function.

The statement of the Plays and Support is:

Bool Plays (lpcstr pszsound, hmodule hmod, dword fdwsound);

The parameter pszsound is a string that specifies the sound to play sound, which may be the name of the WAVE file, or the name of the WAV resource, or a pointer to the sound data in memory, or the system defined in the system registry Win.ini Event sound. If the parameter is NULL, stop the sound being played. The parameter hmod is an instance handle of the application that uses this parameter when playing the WAV resource, otherwise it must be NULL. The parameter fdwsound is a combination of flags, as shown in Table 11.5. If the function returns true if the function returns true, otherwise returns false.

Table 11.5 Playback Sign

The symbol meaning SND_Application uses the association specified by the application to play sound. The SND_ALIAS PSZSound parameter specifies the alias of the system events in the registry or Win.ini. The SND_ALIAS_ID PSZSound parameter specifies a predefined sound identifier. SND_ASYNC plays sound in an asynchronous manner, and the Plays andound function returns immediately after starting playback. The SND_FILENAME PSZSound parameter specifies the WAVE file name. SND_LOOP repeats the sound, must be used with the SND_ASYNC flag. SND_MEMORY playback is loaded into the sound in memory, at which time pszsound is a pointer to sound data. SND_NODEFAULT does not play the default sound, if there is no such sign, Plays and the Plays will play the default sound when they are not found. SND_NOSTOP Plays does not interrupt the original sound broadcast and return to false immediately. SND_NOWAIT If the driver is busy, the function does not play the sound and return it immediately. SND_PURGE stops all sounds related to calling tasks. If the parameter pszsound is null, all sounds are stopped, otherwise, stop pszsound specified. The SND_RESOURCE PSZSound parameter is the identifier of the WAVE resource, and the hmod parameter is used. SND_SYNC is synchronized to play sound, and then the Plays andound function is returned after playing. In the C: / Windows / Media directory, there is a sound file called the Microsoft Sound.wav, which is played in Windows 95. Below we use three ways to call the Plays and Support Sound from Windows 95.

The first method is to broadcast the sound file directly, 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 transfers represent a backslash.

The second method is to add the sound file into the resource and then play the sound from the resource. Visual C supports WAVE type resource, the user is right-click in the resource view and select the import command, and then select the Microsoft Sound.wav file in the file selection dialog box, the file will be added to the WAVE resource. Assume that the ID of the sound resource is IDR_StartWin, then the call below will also output the startup sound:

Playsound ((lpctstr) idR_startwin, AFXGetInstanceHandle (), SND_RESOURCE | SND_ASYNC);

The third method is to play the system sound with the Playsound, and Windows booted sound is the system sound defined by systemStart, so you can play the startup sound with the following method:

Playsound ("SystemStart", NULL, SND_ALIAS | SND_ASYNC);

The function of the function SNDPlaySound is similar to Playsound, but less a parameter. The declaration of the function is:

Bool SndplaySound (LPCSTR LPSZSOUND, UINT FUSOUND);

In addition to not specifying the name of the resource, the parameter LPSZSOUND is the same as Playsound. The parameter fusound is a sign of how to play the sound, which can be SND_ASYNC, SND_LOOP, SND_MEMORY, SND_NODEFAULT, SND_NOSTOP, and SND_SYNC, and the meaning of these flags is the same as Playsound.

It can be seen that SndPlaySound cannot directly play the sound resource. To play a WAVE file with this function, you can call it below:

SNDPLAYSOUND ("MySound.wav", SND_ASYNC);

11.6.2 MCI

MCI (Media Control Interface, Media Control Interface) provides the Windows program with the ability to control the media device interface at a high level. The program does not have to care about the specific device, and media devices such as the laser player (CD), a visible, waveform audio device, video playback device, and MIDI devices can be controlled. For programmers, MCI can be understood as a row button on the device panel, allowing the device to complete various functions by selecting different buttons (send different MCI commands) without having to care about the internal implementation of the device. For example, there are different reactions for Play, a visible and CD (one is playing video, a playback audio), but only needs to press the same button to the user.

The application controls the media device by sending a command to the MCI. The MCI command interface is divided into two types of command messages, both of which have the same function. The command string has a simple feature, but its execution efficiency is not as command message.

All MCI command strings are passed to MCI through multimedia API functions McISendString, and the declaration of this function is:

McIrror McIndstring

LPCTSTR LPSZCOMMAND, / / ​​MCI Command String

LPTSTR LPSZRETURnstring, // Subtiles to store feedback information

UINT CCHRETURN, / / ​​Buffer length

Handle HWndCallback // Toning window handle, generally null

); If it is successful, it returns 0, otherwise returns an error code.

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

Bool mciGeterrorString

DWORD fdwerror, // function McISendString or McSendCommand returned error code

LPTSTR LPSZERRORTEXT, / / ​​Receive buffers of the descriptor string

The length of the uint cchertext // buffer

);

Below is a simple example of using the mcIndstring function:

Char BUF [50]; mcIrror mcierror;

McIrror = McIndstring ("Open CDAUDIO", BUF, STRLEN (BUF), NULL

IF (mcier) {mciGeterrorString (McIgeRR, BUF, Strlen); AFXMESSAGEBOX (BUF); Return;}

Open cdaudio command opens the CD player, if an error (such as the CD is not CD), the error code is returned, and the error message string can be obtained with the MciGeTerRORSTRING function. Open is the command of the MCI to open the device, and CDAUDIO is the MCI device name. MCI's device type is listed in Table 11.6.

Table 11.6 MCI device type

Equipment Type Description Animation Animation Equipment CDAUDIO CD Player DAT Digital Audio Machine DigitalVideo Digital Video (Non-GDI) Other Undefined MCI Device Other Other Mac MCI Device Overlay Device (Analog Video in Window) Scanner Image Scanner Sequencer MIDI sequencer VideoDisc video wheel Waveaudio playing digital wave file audio equipment

Readers should pay attention, device types and device names are different concepts. The device type refers to a class of MCI devices in a group of shared commands, and the device name is the name of a MCI device. The system needs to distinguish different devices that belong to the same device type with different device names.

The device name is defined by the [MCI] section of the registry or System.ini, the typical [MCI] segment is as follows: [MCI] cdaudio = mcicda.drv sequence = mciseq.drv Waveaudio = mciwave.drv avivideo = mciavi. DRV videodisc = MCIPIONR.DRV

The left side of the equal sign is the device name, and the right side is the corresponding MCI driver. When a new MCI driver is installed, the system needs to distinguish between different device names. The device name is usually the same as the device type name in the driver, such as CDAUDIO and WAVEAUDIO, but also exception, such as the avivideo device is a DigitalVideo type device.

Using MCI devices typically include open, use, and closes three processes. Most of the MCI can control different media devices. For example, you can play a WAVE file, video file or CD with a play command. Table 11.7 lists commonly used MCI command strings, and most of the commands are versatile. Behind the MCI command generally wants to with a device name to specify an object.

Table 11.7 Common MCI commands

Command Description Capacility Query Device Capacity CLOSE Close Device Info Query Device Open Open Device PAUS Pause Device Playback or Recording Play Start Device Play Record Start Resume Restore Suspension Play Or Recording Device Seek Change Media Current Location STATUS Query Device Status Information STOP Stop Device Play or Record

For example, after the above example opens a CD player, you can send a common command to control the CD machine:

Play CDAUDIO from TO . If the FROM is omitted, playback from the current track, if the TO is omitted, playback to the end.

Pause cdaudio. Pause playback.

Stop cdaudio. Stop play.

Resume cdaudio. Continue to be paused.

Status Cdaudio Number of Tracks. Query the number of CDs. Status CDAUDIO CURRENT TRACK can query the current track.

Seek CDAUDIO TO . Move to the specified track.

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

Close Cdaudio. Close the device.

The MCI device can be classified according to simple devices and composite devices. Like the device like CDAUDIO, we call it simple device, and the composite device uses data files, such as digital video (DigitalVideo) and Waveaudio), which we will call these data files. .

Specify the device name and device elements when you open a composite device. For example, the following command opens a waveform audio device:

Open mysound.wav Type Waveaudio

You can only specify device elements for the composite device, for example:

Open mysound.wav

As shown below, the system can determine which device opens by looking for [MCI extensions] by looking up the registry or WININI.

[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 simultaneously on the screen. In this case, you need a different alias for each open device, so MCI can distinguish between two playback devices. For example, the following code opens and played two AVI files:

Char BUF [50]; McIndstring ("Open Dillo.avi Type Avideo Alias ​​Dillo", BUF, STRLEN (BUF), NULL); McISendString ("Play Dillo Repeat", BUF, Strlen (BUF), NULL); // Repeat Play

McISendstring ("Open Search.avi Type Avodeo Alias ​​Search, BUF, Strlen (BUF), NULL); McIndstring (" Play Search ", BUF, STRLEN (BUF), NULL

When using the Open command to open the device, if an alias is specified, the operation of the device will use an alias.

So far, we are using the MCI command string. The reader may have this experience, the command string has the advantages of simple and easy to learn, but this interface is far from the C / C style, if the program is to query and set a lot of data, then the form of the string will be very inconvenient.

The MCI command message interface provides a C language interface, which is faster and more complies with C / C programmers. All MCI command messages are sent through the McISendCommand function, and the declaration of this function is:

McIrror McISendCommand

McIDeviceID IDDevice, // device ID, does not need this parameter when turning on the device

UINT UMSG, // Command Message

DWORD fdwcommand, // command message sign

DWORD dwparam // point to the structure of the command message parameter

); // Returns 0 if successful, otherwise returns an error code

Listing 11.8 code demonstrates an AVI file with an MCI command message to open and repeat:

Listing 11.8

MCI_dgv_open_parms mciopen; uint wdeviceid; mcierror mcierror;

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

Mciopen.lpstreamentname = "dillo.avi"; // equipment element

McIrror = McISendCommand (0, MCI_Open,

MCI_open_type | mci_open_element, // uses device elements

(DWORD) & mciopen; if (mcierror) {char s [80]; mciGeterror, S, 80); AFXMESSAGEBOX (s); Return;}

WDEVICEID = mciopen.wdeviceID; // Save device ID

MCI_DGV_Play_Parms McIPlay; mcierror = mciSendCommand (WDEVICEID, MCI_Play, MCI_DGV_Play_repeat, (DWORD) & mciplay);.

It can be seen that the command message is more complicated than using the command stroke. The command message is corresponding to the command string, for example, Open is the same function as MCI_Open. Variable WDEVICEID is used to save the device ID, the system uses ID to identify different devices to ensure that the command is sent to the correct object.

Limited to the space, the command message for MCI is not described in detail.

http://www.lzu.edu.cn/netteach/jiaochen/vc 5.0/vc 5.0/chap11/chap11_6.htm

转载请注明原文地址:https://www.9cbs.com/read-8537.html

New Post(0)