How to play a large WAV file

Source: Internet
Author: User
Tags data structures resource

Usually, how do you deal with sound files in the design of multimedia software? Use the API function sndPlaySound provided by Windows to implement WAV file playback? However, have you ever encountered this situation: when the WAV file is larger than the available memory, the sndPlaySound function can not play!!! So, how do you use MCI to play large audio files?

This article will introduce a method.

Windows supports two types of riff (Resource Interchange File format, resource Interactive file formats) audio files: Midi rmid files and waveform audio file format wav files, this article describes how to use the MCI command to play large w av files. Playing an audio file with sndPlaySound requires only one line of code. For example, the method of realizing asynchronous playback is sndPlaySound ("C:\windows\ding.wav", Snd_async);

From this we can see that the use of sndPlaySound is very simple. However, there is a limit to playing audio files with sndPlaySound, that is, the entire audio file must be fully tuned into the available physical memory. Therefore, the application of sndplaysound playback of the audio file is relatively small, the most about 100K. The ability to use MCI is required to play larger audio files (which are frequently encountered in multimedia design). This creates a Cwave class that handles the MCI command that plays audio, because it performs many MCI commands and establishes data structures, so only simple member functions (such as Opendevice, Closede Vice, play, and stop) are required. Specific MCI commands and data structures are abstracted in the Cwave class, containing only a few simple member functions Opendevice, CloseDevice, play and stop. Waveform audio device is a composite device, if you turn on the waveform device, and then turn on and off each waveform element, and finally turn off the waveform device, which can make playback performance better. Call C Wave::opendevice to open the waveform device, Opendevice will mci_open command passed to the Mcisendcommand function, if the call succeeds, use the data structure Mci_open_ The Wdeviceid member of the Parms returns the identifier for the waveform device, which is stored in a private data member for later use. Once the Cwace object is open, a WAV file is played through the cwace::P la y, and the WAV filename and a window pointer are passed to the play method to send the MCI notification message to the established window.

WAV files are played first by assigning a mci_open_parms structure and setting the WAV file to play

Lpstrelementname member to open WAV file. Pass the structure and Mci_open to Mcisendcommand, open the WAV file and return the element identifier with the Wdeviceid member of the MCI_OPEN_PARMS structure. The second step is to command waveform audio devices to play WAV files. The mci_play_parms structure is assigned and the Dwcallback member is set to a window handle. If you want to play the audio waveform file synchronously, increase the MCI_WAIT flag and skip the window handle. Doing so will allow the application to wait for WAV files to complete before the Mcisendcomman D function returns. The most likely scenario is to play a large WAV file asynchronously, and you can specify the MCI_NOTIFY flag as follows and set the Dwcallback member to do so.

MCI_PLAY_PARMS mciPlayParms;
  MciPlayParms.dwCallback=(DWORD)pWnd->m_hWnd;
  DwResult=mciSendCommand(m_nDevice,
              MCI_PLAY,
              MCI_NOTIFY,
              (DWORD)(LPVOID)&mciPlayParms);
 

This starts the WAV file playback, and after the playback is finished, the mm_mcinotify message is sent to the specified window. A WAV file playback event sequence is: (1) command to play WAV file and return immediately, (2) Play WAV file, (3) Send a notification message when finished.

It is the programmer's responsibility to turn off the WAV file element after playback and simply call the stop member function of the Cwave class. The Stop member function passes the WAV file identifier and the Mci_close command to the Mcisendcommand function, you do not have to assign a MCI structure to the command, and the following code closes the WAV file:

mciSendCommand(m_nElement,MCI_CLOSE,NULL,NULL);

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.