Playback of audio files in VC

Source: Internet
Author: User
Tags what header
Document directory
  • 1. Advanced Audio functions.
  • 2. MCI
Playback of audio files in VC

Yang Chen

Introduction

First of all, I have not learned well and feel that I have not been able to fulfill the task assigned by the party.

 

I haven't watched any sound during the past few days. On the one hand, it seems that we have enough knowledge from the five sub-scripts we have to complete and the bomb program. On the other hand, playing audio in VC is a very complicated task. I don't want to spend too much time on it. In addition, I wrote this article (which should be a casual article, and where should I write it? Otherwise, I am sorry) writing it one day earlier does not affect everyone, after all, this stuff is independent from our program.

 

Well, because I have read a lot of things, and I have no authoritative materials at hand, I read some msdn stuff, but I only understand some of them, I still don't know if it's right, so I can't talk about a lot of things, because it may mislead you. Most of the content below is based on the opinions of some strong people on the Internet or in some books. Of course, there are also a lot of my own languages. g. if you want to look at some places, you can ignore it.

 

OK.

 

Visual c ++ supports multimedia

For general applications, Visual C ++ can be said to be all-encompassing, but unfortunately, almost never heard of Visual C ++'s support for multimedia, some people even say that visual C ++ is not suitable for multimedia programming. If we fully use the Visual C ++ class library without any tricks, I'm afraid we won't be able to compile the most simple RPG Game. For a multimedia application that requires a large number of animations and sounds, visual C ++ provides a shell at most, and compiles a task of excellent sound and animation engines, it falls into the programmer.

 

The above is not what I said. I went on to say: Then, how to develop this engine is likely to fall into your head in the future. You need to organize classes for your applications.

 

Big ones are not very good. Small applications should be fully compatible.

 

Windows Multimedia Service Functions

Windows 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. Here we will briefly introduce some of the most commonly used multimedia services.

 

1. Advanced Audio functions.

Messagebeep, playsound, and sndplaysound. You should be familiar with these three functions. I don't want to say too much here. Because I feel very simple, you can check msdn. Pay attention to what header files they need, what the parameters are for, and what file formats are limited. For example, none of them can play waveform sound files larger than kb.

To write an example, you should be clear:

 

Playsound (" ..wav", null, snd_async | snd_loop );

// The first parameter is the sound file name, and the last snd_loop indicates loop playback.

 

I feel that it is impossible to use these functions to play two types of music simultaneously. No matter you use it without thread control, if you use it once, it will occupy the interface of the device. If you use it for the second time, you may have snatched the interface for the first time and cannot use it, you can't take it away, so you can pick it up if you don't want to use it.

 

2. What is MCI?

Media control interface (MCI) media control interface is a set of standard interfaces for multimedia devices and files provided by Mircrosoft, its advantage is that it can easily control the vast majority of multimedia devices, including audio, video, album, video and other multimedia devices, without the need to know their internal working conditions. But the ancient saying goes: success and failure. Although it seems to be a great deal, it is far from enough for some advanced applications.

 

The above is not what I said, but the low-level and intermediate applications are totally compatible.

 

MCI provides Windows programs with the ability to control media device interfaces at a high level. 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.

Usage of MCI

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. (Mcisendstring, a string message. Mcisendcommand, Command Message)

 

1. mcisendstring first.

On the surface, it means to send a string, that is, to tell some devices, please play it !, Note that the first one of the following parameters is enclosed in double quotation marks, but there is a fixed format in the quotation marks.

 

First, write a simple example so that you will not be confused.

 

Mcisendstring ("Open rat love rice. Avi type device 1 ",...);

Mcisendstring ("Play Device 1 repeat ",...);

 

I wrote this example randomly. You should suddenly feel the blue sky on your head. You can understand what it means. What does repeat mean. That is to say, you really like that song. You have to listen to it once.

Let's talk about it. The first sentence is to send a command, put the mouse into the device, and then the second sentence is to send a command to start running for me!

 

 

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

 

Mcierror mcisendstring (// mcierror is only a return type. Do not be afraid.

// Command character (just now ...... Previous: "Open rat love rice. Avi type device 1 ")

Lpctstr lpszcommand,

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.

 

 

// You can just take a look at the following. For error handling, it's okay if you don't need to write a program. Just copy it.

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

);

 

// You can only take a look at the red part.

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;

}

Mcisendstring ("Open rat love rice. Avi type device 1 ",...);

Mcisendstring ("Play Device 1 repeat ",...);

 

In this way, you can listen to the mouse and love rice, but if I want to listen to the [rice and mouse] song, I want to play two songs together, then I will add a mcisendstring ("Open rice loves mice. avi type device 1 ",...); Yes?

 

This is not acceptable.

In this case, you need to create a different alias for each device that is opened, so that the MCI can distinguish two playback devices.

 

For example, the following code opens and plays two AVI Files: (alias is an alias .)

 

Char Buf [50];

Mcisendstring ("Open rat love rice. Avi type avivideo alias ADU", Buf, strlen

(BUF), null );

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

Mcisendstring ("Open rice love mouse. Avi type avivideo alias guanghao", Buf, strlen

(BUF), null );

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

 

In this way, the background music and normal music are played simultaneously.

2. mcisendcommand

I don't have to say much about this. Write an example:

Basically, the following is a fixed format. If you want to open and upload a .wav file, you only need to replace the following two blue parts

Waveaudio and Windows XP can be shut down to .wav.

Mci_dgv_open_parms mciopen;

Uint wdeviceid;

Mcierror;

 

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

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

Mcierror = mcisendcommand (0, mci_open,

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

(DWORD) & mciopen );

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

Mci_dgv_play_parms mciplay;

Mcierror = mcisendcommand (wdeviceid, mci_play, mci_dgv_play_repeat,

(DWORD) & mciplay );

 

These programs contain many header files. I have never summarized anything. For example

# Include <mmsystem. h>;
# Include <digitalv. h>
# Include <VFW. h>
# Pragma comment (Lib, "vfw32.lib ")

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

 

When I use this function, the program will not report an error. I will comment it out first, and then reveal a line. If an error is reported, see which function is used, check msdn and put the header file that needs it.

 

In addition, I feel that many strong online players use the following method to play audio files. mciwndcreate and mciwndplay. Of course, they also use the MCI class. This is really convenient and simple. Haha.

For example:

Hwnd m_hwndmci;

If (m_hwndmci! = NULL) // create a mciwnd window

{

Mciwnddestroy (m_hwndmci );

}

Int type = 2; // hide the default toolbar. Table 0 is displayed.
Cstring filename = ""; // file name to be played
M_hwndmci = mciwndcreate (m_hwnd, AfxGetInstanceHandle (), type, filename );

Mciwndplay (m_hwndmci );

Add the following to stdafx. h:

# Include <VFW. h>
# Pragma comment (Lib, "vfw32.lib ")

For more control functions, open the VFW. h file.

For example:

Mciwndgetposition (m_hwndmci) to get the current playback position, used to control the playback progress Slider
Mciwndgetlength (m_hwndmci) file playback Length
Mciwndsetvolume (m_hwndmci, ivol) sets the volume, the size is ivol, the maximum value is 1000
Mciwndgetvolume (m_hwndmci) obtains the current volume.
Mciwndplayfromto (m_hwndmci, lstart, lend) video clip

 

Let's talk about this first. Work together and make progress together.

 

-------------------------------------

Sangmin

7/27/2005 Yu Ganyu

 

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.