Using the Waveout function family to play WAV files under Windows

Source: Internet
Author: User

To use the Waveout function group, the first thing to know about a few data structures is this

typedefstructtwaveformatex{WORD wFormatTag; /*type of format*/WORD nchannels; /*Number of channels*/DWORD nsamplespersec; /*Sampling Frequency*/DWORD navgbytespersec; /*bit Rate Sample rate * Number of channels * Sample bits/8*/WORD nblockalign; /*number of width channels per sample * Number of sample bits/8*/WORD wBitsPerSample; /*number of sample bits*/WORD cbsize; /*external additional information, not set to 0*/                                    /*Extra Information (after cbsize)*/} WaveFormatEx,

This structure is necessary to open the audio device when it is ready to play sound, and the function to be used when opening the audio device is

if (! (Waveoutopen (lphwaveout) &hwaveout, Wave_mapper, (Lpwaveformatex) &pformat, (DWORD) This0 0) = = Mmsyserr_noerror))

The first parameter is the handle to the audio device, as defined below

Lphwaveout hwaveout;

is actually a device pointer

The second parameter is the device type, where select Wave_mapper can choose a waveform audio output device that can play to the fixed type

The third parameter is the format information for the audio data

A fourth parameter is a callback function

The fifth one is the resource handle of the callback function

The sixth is to open the device's identity, the specific description can go to see MSDN successfully returned Mmsyserr_noerror, otherwise return the following values:

Value Description
Mmsyserr_allocated indicates that a resource already exists
Mmsyserr_baddeviceid Device ID out of range
Mmsyserr_nodriver No Drive
MMSYSERR_NOMEM Cannot allocate memory
Waverr_badformat attempt to open an unsupported format
Waverr_sync devices can be synchronized, but Waveoutopen useless wave_allowsync

After you open the audio device, you need to prepare the audio format information, which requires a second structure

/*Wave data Block header*/typedefstructWavehdr_tag {LPSTR lpdata; /*pointer to the lock data buffer*/DWORD dwbufferlength; /*Data buffer length*/DWORD dwbytesrecorded; /*used for input only*/dword_ptr Dwuser; /*For client ' s use*/DWORD dwFlags; /*Assorted flags (see defines)*/DWORD Dwloops; /*Loop Control Counter*/    structWavehdr_tag far *lpnext;/*reserved for driver*/dword_ptr reserved; /*reserved for driver*/} WAVEHDR

Use the following methods

HWAVEHDR = GlobalAlloc (gmem_moveable | Gmem_share, (DWORD)sizeof(WAVEHDR)); if(Hwavehdr = =NULL) {MessageBox ("Not enough memory for header","Error", MB_OK);//failed to turn on the device            return; } LPWAVEHDR=(LPWAVEHDR) GlobalLock (HWAVEHDR); if(Lpwavehdr = =NULL) {MessageBox ("Failed to lock memory for header","Error", MB_OK); return; }        //Initialize the audio structure after allocation, set up and prepare header.Lpwavehdr->lpdata = (Char*) Pvoicebuffer; LPWAVEHDR->dwbufferlength =lfilesize; LPWAVEHDR->dwflags =0L;//Whdr_beginloop | Whdr_endloop;Lpwavehdr->dwloops =0L; if(Waveoutprepareheader (hwaveout) hwaveout, LPWAVEHDR,sizeof(WAVEHDR)) ! = Mmsyserr_noerror)

The buffer can be set to the memory address of the data we read in, and the buffer needs to be locked

Then we can write the data.

sizeof (WAVEHDR));

Write data fails to remove the previously locked buffer, use this function

sizeof (WAVEHDR));            GlobalUnlock (LPWAVEHDR);            GlobalFree (HWAVEHDR);

If successful you can go into the loop and play until you know the data is playing, as follows

 while(Waveoutunprepareheader (hwaveout) hwaveout, LPWAVEHDR,sizeof(WAVEHDR))! =mmsyserr_noerror) {            //Loop Clear Buffer If successful means playback is complete//Loop The following statement if it has not finished playing            if(bexit) Break;//let loops also respond to messagesMSG message; if(::P eekmessage (&message,null,0,0, Pm_remove)) {:: TranslateMessage (&message); ::D Ispatchmessage (&message); }         }

After the data playback is completed, the player needs to be reset before the audio device is turned off, as follows

//Play finished        if(Waveoutreset (hwaveout) hwaveout)! =mmsyserr_noerror) {MessageBox ("Waveoutreset error!","Error", MB_OK); return; }        if(Waveoutclose (hwaveout) hwaveout)! =mmsyserr_noerror) {MessageBox ("waveoutclose error!","Error", MB_OK); return; }

Through these can complete a basic WAV player, while the MP3 player only needs MP3 decoding can also do

The interface is as follows

Project code links are as follows

http://download.csdn.net/detail/dengrengong/8639579

Using the Waveout function family to play WAV files under Windows

Related Article

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.