Ways to play sounds in MFC

Source: Internet
Author: User
Tags data structures error handling

Sound is an important part of multimedia, and adding sound to an application can make the interface more friendly. In VC + + can be based on different application requirements, with different methods to achieve sound playback.

A A simple way to play a sound file

A set of functions related to audio devices is provided in the multimedia Dynamic Connection library in VC + +. These functions make it easy to play sounds. The simplest way to play sounds is to directly call VC + + provided in the sound Play function bool sndPlaySound (LPCSTR lpszsound,uint fusound); or bool PlaySound (LPCSTR lpszsound, hmodule hmod, DWORD fusound), where the parameter lpszsound is the path and file name of the. wav file that needs to play the sound, Hmod here is null, Fusound is a sign of playing sound, please refer to VC + + Help for detailed instructions. For example, play c:soundmusic.wav can be used with sndPlaySound ("C:\sound\music.wav", Snd_async), or PlaySound ("C:\sound\music.wav", NULL, Snd_ async| Snd_nodefault); If the Music.wav file is not found, the first format will play the system default sound, and the second format will not play the system default sound.

Two Add a sound file to your program

In VC + + program design, you can use a variety of standard resources, such as bitmaps, menus, dialog boxes and so on. At the same time, VC + + allows users to customize resources, so we can use the sound file as a user-defined resource into the program resource file, compiled connection generated EXE file, achieve none. WAV file for sound playback.

To implement the playback of a sound file as a resource, first add the sound file to be played in the Resource Manager (the implementation process is not complicated, not described here). Assume that the generated sound file resource identifier is idr_wave1. You only need to invoke the following statement when playing:

PlaySound (Makeintresource (idr_wave1), AfxGetResourceHandle (), snd_async| snd_resource| Snd_nodefault| Snd_loop);

where the Makeintresource () macro transforms an integer resource identifier into a string, the AfxGetResourceHandle () function returns the module handle containing the resource,

Snd_resource is a must-have sign.

The second way to play a sound file as a resource is to read the resource into memory and play it as memory data. Concrete steps into the following:

1. Get the module handle containing the resource:

Hmodule Hmod=afxgetresourcehandle ();

2. Retrieving resource block information:

Hrsrc Hsndresource=findresource (Hmod,makeintresource (idr_wave1), _t ("WAVE"));

3. Load the resource data and lock it in:

Hglobal Hglobalmem=loadresource (Hmod,hsndresource);

LPCTSTR lpmemsound= (LPCSTR) Lockresource (HGLOBALMEM);

4. To play a sound file:

sndPlaySound (lpmemsound,snd_memory));

5. To release a resource handle:

FreeResource (HGLOBALMEM);

Three Advanced methods for playing sound files

In VC + + provides a set of audio equipment and multimedia files directly to operate the function. These functions allow for flexibility in the processing of sound files.

First, we introduce several data structures to be used. The WAVEFORMATEX structure defines the format of the Wave audio data file. The WAVEHDR structure defines the waveform audio buffers. The read data is first populated in order to send the audio device to play. The WAVEOUTCAPS structure describes the performance of the audio device. The MMCKINFO structure contains information about a block in the riff file. Please refer to the Help in VC + + for detailed instructions.

The following gives the program flow diagram and program source code list, in VC + + environment can be directly used:

The source program list is as follows:

LPSTR szfilename;//sound file name

Mmckinfo mmckinfoparent;

Mmckinfo Mmckinfosubchunk;

DWORD dwfmtsize;

Hmmio m_hmmio;//Audio file handle

DWORD M_wavelong;

HPSTR lpdata;//Audio data

HANDLE M_hdata;

HANDLE M_hformat;

WaveFormatEx * LPFORMAT;

DWORD M_dwdataoffset;

DWORD m_dwdatasize;

WAVEHDR Pwaveouthdr;

Waveoutcaps Pwoc;

Hwaveout hwaveout;

Open a waveform file

if (! ( M_hmmio=mmioopen (szfilename,null,mmio_read| MMIO_ALLOCBUF)))

{

File Open Error

Error ("Failed to open the file."); /Error Handling functions

return false;

}

Check if open file is a sound file

Mmckinfoparent.fcctype =MMIOFOURCC (' W ', ' A ', ' V ', ' E ');

if (Mmiodescend (M_hmmio, (lpmmckinfo) &mmckinfoparent,null,mmio_findriff))

{

Not WAVE FILE and QUIT

}

Find the ' FMT ' block

Mmckinfosubchunk.ckid =MMIOFOURCC (' f ', ' m ', ' t ', ');

if (Mmiodescend (M_hmmio,&mmckinfosubchunk,&mmckinfoparent,mmio_findchunk))

{

Can ' t find ' fmt ' chunk

}

Get the ' FMT ' block size, request memory

Dwfmtsize=mmckinfosubchunk.cksize;

M_hformat=localalloc (Lmem_moveable,loword (dwfmtsize));

if (!m_hformat)

{

Failed Alloc memory

}

lpformat= (waveformatex*) LocalLock (M_hformat);

if (!lpformat)

{

Failed to lock the memory

}

if ((unsigned long) mmioread (M_hmmio, (HPSTR) lpformat,dwfmtsize)!=dwfmtsize)

{

Failed to read format chunk

}

Leave the FMT block

Mmioascend (m_hmmio,&mmckinfosubchunk,0);

Looking for ' data ' blocks

MMCKINFOSUBCHUNK.CKID=MMIOFOURCC (' d ', ' a ', ' t ', ' a ');

if (Mmiodescend (M_hmmio,&mmckinfosubchunk,&mmckinfoparent,mmio_findchunk))

{

Can ' t find ' data ' chunk

}

Get the size of the ' data ' block

M_dwdatasize=mmckinfosubchunk.cksize;

M_dwdataoffset =mmckinfosubchunk.dwdataoffset;

if (m_dwdatasize==0l)

{

No data in the ' data ' chunk

}

Allocating memory for audio data

Lpdata=new Char[m_dwdatasize];

if (!lpdata)

{

Faile

}

if (Mmioseek (m_hmmio,soundoffset,seek_set) <0)

{

Failed to read the data chunk

}

M_wavelong=mmioread (M_hmmio,lpdata,soundlong);

if (m_wavelong<0)

{

Failed to read the data chunk

}

Check the audio device to return the performance of the audio output device

if (Waveoutgetdevcaps (wave_mapper,&pwoc,sizeof (waveoutcaps))!=0)

{

Unable to allocate or lock memory

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.