Method for playing sound in VC ++

zhaozj2021-02-16  106

Method of playing sounds in VC : Li Canwei Article Summary: This article discusses several different ways to play a sound file in VC , and provides concrete realization sentence. Keywords: sound file audio device playing multimedia

-------------------------------------------------- ------------------------------ Text:

Method for playing sound in VC

Sound is an important part of multimedia, adding sound in the application to make the interface more friendly. In VC , you can achieve sound playback in different ways according to different applications.

One. A simple way to play sound files provides a set of functions related to audio devices in the multimedia dynamic connection library in VC . Use these functions to easily play sound. The simplest playback sound method is to directly call the sound playback function Bool SndPlaySound (LPCSTR LPSZSOUND) provided in VC ; or Bool Plays (LPCSTR LPSZSOUND, HMODULE HMOD, DWORD FUSOD); where the parameter lpszsound is required to play sound. WAV The path and file name of the file, HMOD is null here, Fusound is the flag of playing sound, please refer to the help in VC for details. For example, playing c: /ason/music.wav can use SndPlaySound ("C: //Sound/Music.wav", SND_ASYNC), or Plays ("C: //Sound/Music.wav", NULL, SND_ASYNC | SND_NODEFAULT; if there is no Music.WAV file, the first format will play the system default sound, and the second format does not play the system default sound.

two. Add a sound file to the program in the program in VC , you can take advantage of the resources such as bitmaps, menus, dialogs, and more. At the same time, VC also allows users to customize resources, so we can use the sound file as a user-defined resource to join the program resource file, and generate the exe file through the compilation connection to achieve the sound playback of the .wav file. To achieve the playback of the sound file as a resource, you must first add the sound file to be played in the Explorer (the implementation process is not complicated, not described herein). Assuming that the generated sound file resource identifier is IDR_Wave1. When playing only need to call the following statement: PlaySound (MAKEINTRESOURCE (IDR_WAVE1), AfxGetResourceHandle (), SND_ASYNC | SND_RESOURCE | SND_NODEFAULT | SND_LOOP); where MAKEINTRESOURCE () macro to convert the integer to a string resource identifier, AfxGetResourceHandle () function returns The module handle of the resource, SND_RESOURCE is a must-have flag. The second play method of the sound file as the resource is to play the resource after the memory is played as a memory data. The specific steps are entered: 1. Get a module handle containing resources: hmodule hmod = AFXGETRESOURCEHANDLE (); 2. Retrieving resource block information: HRSRC hSndResource = FindResource (hmod, MAKEINTRESOURCE (IDR_WAVE1), _ T ( "WAVE")); 3. locking and load the resource data: HGLOBAL hGlobalMem = LoadResource (hmod, hSndResource); LPCTSTR lpMemSound = (LPCSTR) LockResource (HglobalMem); 4. Play sound file: SNDPlaySound (lpMemsound, SND_MEMORY); 5. Release the resource handle: FreeResource (HglobalMem); Third. The advanced method of playing sound files provides a set of functions that do direct operations directly to audio devices and multimedia files in VC . Use these functions to flexibly perform a variety of processes. First 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 buffer. The read data first is to populate this buffer to send audio devices to play. The WaveoutCaps structure describes the performance of the audio device. The MMCKInfo structure contains information of a block in the Riff file. For a detailed description, please refer to the help in VC . The procedure flow diagram and program source code list will be given directly in the VC environment:

Source 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 the waveform file if | {// file open ErrorError ( "Failed to open the ((m_hmmio = mmioOpen (szFileName, NULL, MMIO_READ MMIO_ALLOCBUF))!) File. "); // Error handler Return False;} // Check if the file is sound file mmckinfoparent.fcctype = mmiofourcc ('w', 'a', 'v', 'e'); if (MmioDescend) M_hmmio, (lpmmckinfo) & mmckinfoparent, null, mmio_findriff) {// not Wave file and quit} // Looking for 'FMT' block mmckinfosubchunk.ckid = mmiofourcc ('f', 'm', 't', '); if (mmioDescend (m_hmmio, & mmckinfoSubChunk, & mmckinfoParent, MMIO_FINDCHUNK)) {// Can not find 'fmt' chunk} // get 'fmt' block size, application 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} // fmt block away mmioAscend (m_hmmio, & mmckinfoSubChunk, 0); // Find 'data' block mmckinfoSubChunk.ckid = mmioFOURCC ( 'd', 'a', 't', 'a'); if (mmioDescend (m_hmmio, & mmckinfoSubChunk, & mmckinfoParent, MMIO_FINDCHUNK)) {// Can not find 'data' CHUNK} // Get the size m_dwdatasize = mmckinfosubchunk.cksize; m_dwdataoffset = mmckinfosubchunk.dwdataoffset; if (m_dwdatasize == 0L) {// no data in the 'data'

CHUNK} // Distributes memory LPDATA = new char [m_dwdatasize] for audio data; if (! lpdata) {// faile} f (mmioseek (m_hmmio, snoudoffset, 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, an audio output device returns performance if (waveOutGetDeVCaps (WAVE_MAPPER, & pwoc, sizeof (WAVEOUTCAPS )))! = 0) {// unable to allocate or lock memory} // check if the audio output device can play the specified audio file IF (Waveoutopen (& Hwaveout, DEVSNUM, LPFORMAT, NULL, NULL, CALLBACK_NULL)! = 0) { // Failed to OPEN the wave out devices} // prepare data to be played pWaveOutHdr.lpData = (HPSTR) lpData; pWaveOutHdr.dwBufferLength = m_WaveLong; pWaveOutHdr.dwFlags = 0; if (waveOutPrepareHeader (hWaveOut, & pWaveOutHdr, sizeof (WAVEHDR) )! = 0) {// failed to prepare the Wave data buffer} // Play audio data files IF (Waveoutwrite (Hwaveout, & PWaveoutHDR, Sizeof (WaveHDR))! = 0) {// Failed to Write the Wave Data Buffer} // Turn off the audio output device, release memory WaveoutreSet (hWaveout); localunlock (m_hformat); LocalFree (m_hformat); delete [] lpdata; description: 1) A declaration of audio devices and sound file operation functions used above Contains in the MMSystem.h header file, so in the program must be used in the program. "Mm System.h "statement is added to the header file. At the same time, you should add a dynamic connection to the library WinMM.Lib. The specific implementation method is to select Settings from the developer studio's Project menu, then add WinMM.LIB in the Object / Library Modules control on the Link tab. 2) Specify different data in PWAVEOTHDR.LPDATA, which can play the sound of any specified position in the audio data file. 3) The above programs are debugged in VC 6.0, and the processing of errors and abnormal conditions is omitted in the text, and it must be added in practical applications. four. Conclusion You can play a sound file in VC to play a sound file according to the needs. Simple application can call the sound playback function directly. The second method can add sound as a resource to the executable. If you want to process the sound data before playing, the third method can be used. Reference Bibliography: 1. Mei Paul Perry Chen Xiangquan and other translations "Multimedia Development Guide" Tsinghua University Press 2. Beautiful Peter Norton, Rob McGregor Sun Fengying et al "MFC Development Windows95 / NT4 Application" Tsinghua University Press 1998. Zhou Jingli "multimedia sound card Technology and Application "Electronic Industry Press 1998

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

New Post(0)