Wince6.0 audio playback Problems

Source: Internet
Author: User
Tags what header
Record: Question: How can I play a WAV, WMA, or MP3 audio in wince? What is the specific function? I use playsound (text ("\ Sounds \ bell.wav "), null, snd_sync | snd_nodefault); why not? I put \ Sounds \ bell.wav directly under the root directory of the project folder.
After I remove snd_nodefault, I only play a "beep" sound. It is estimated that it is a system sound. You can also use sndplaysound () to answer the same question. Why: if you can play the default system sound, it should be using snd_filename, or text (http://www.cnblogs.com/buffer/admin/file:////sounds//bell.wav) path is wrong. i. simple Method for playing audio files
A set of functions related to audio devices are provided in the multimedia dynamic Connection Library of VC ++. These functions can be used to conveniently play the sound. The simplest way to play a sound is to directly call the sound playing function bool sndplaysound (lpcstr lpszsound, uint fusound) provided in VC ++; or bool playsound (lpcstr lpszsound, hmodule hmod, DWORD fusound), where the parameter lpszsound needs to be played. the path and file name of the wav file. hmod is null here, And fusound is the symbol of the playing sound. For details, see the help in VC ++. For example, to play c: \ sound \ music.wav, you can use sndplaysound ("C: \ sound \ music.wav", snd_async) or playsound ("C: \ sound \ music.wav ", null, snd_async | snd_nodefault audio streams if the music.wav file is not found, the default sound of the system is played in the first format, and the default sound of the system is not played in the second format.

2. add the sound file to Program
in VC ++ programming, various standard resources can be used, such as bitmap, menu, and dialog box. At the same time, VC ++ also allows users to customize resources. Therefore, we can add audio files as user-defined resources to program resource files and generate EXE files after compilation connection to achieve no. audio Playback of wav files.
to play the audio file of a resource, you must first add the audio file to be played in the Resource Manager (the implementation process is not complicated, but is not described here ). Assume that the generated audio file resource identifier is idr_wave1. You only need to call the following statement during playback:
playsound (makeintresource (idr_wave1), afxgetresourcehandle (), snd_async | snd_resource | snd_nodefault | snd_loop);
makeintresource () the macro converts an integer Resource Identifier to a string. The afxgetresourcehandle () function returns a module handle containing the resource.
snd_resource is a required identifier.
the second method for playing audio files of resources is to read the resources into the memory and play back the data in the memory. Step 1:
1. obtain the module handle containing the resource:
hmodule hmod = afxgetresourcehandle ();
2. retrieve resource block information:
hrsrc hsndresource = findresource (hmod, makeintresource (idr_wave1), _ T ("wave");
3. load and lock resource data:
hglobal hglobalmem = loadresource (hmod, hsndresource);
lpctstr lpmemsound = (lpcstr) lockresource (hglobalmem);
4. play audio files:
sndplaysound (lpmemsound, snd_memory);
5. release resource handle:
freeresource (hglobalmem);

Iii. advanced methods for playing audio files
In VC ++, a set of functions are provided to directly operate audio devices and multimedia files. Using these functions, You can flexibly Process audio files.
First, we will 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 must be filled in the buffer before the audio device can play the video. The waveoutcaps Structure describes the performance of audio devices. The mmckinfo structure contains the information of a block in the riff file. For more information, see help in VC ++.
The following is a brief diagram of the program process and the programSource codeConfiguration, which can be used directly in the VC ++ environment:

The source program list is as follows:
Lpstr szfilename; // audio 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;
// Open the waveform file
If (! (M_hmmio = mmioopen (szfilename, null, mmio_read | mmio_allocbuf )))
{
// File open Error
Error ("failed to open the file."); // error handling function
Return false;
}
// Check whether the opened 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
}
// Obtain the size of the 'fmt' block and apply for 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
}
// Exit the FMT Block
Mmioascend (m_hmmio, & mmckinfosubchunk, 0 );
// Search for 'data' Blocks
Mmckinfosubchunk. ckid = mmiofourcc ('D', 'A', 't', 'A ');
If (mmiodescend (m_hmmio, & mmckinfosubchunk, & mmckinfoparent, mmio_findchunk ))
{
// Can't find 'data' chunk
}
// Obtain the size of the 'data' Block
M_dwdatasize = mmckinfosubchunk. cksize;
M_dwdataoffset = mmckinfosubchunk. dwdataoffset;
If (m_dwdatasize = 0l)
{
// No data in the 'data' chunk
}
// Allocate 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 and return the performance of the audio output device.
If (waveoutgetdevcaps (wave_mapper, & pwoc, sizeof (waveoutcaps ))! = 0)
{
// Unable to allocate or lock memory
}
// Check whether 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 the 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 the audio data file
If (waveoutwrite (hwaveout, & pwaveouthdr, sizeof (wavehdr ))! = 0)
{
// Failed to write the wave data buffer
}
// Disable the audio output device and release the memory
Waveoutreset (hwaveout );
Waveoutclose (hwaveout );
Localunlock (m_hformat );
Localfree (m_hformat );
Delete [] lpdata;
Note: 1) the declaration of the above audio device and sound file operation function is included in mmsystem. the # include "mmsystem. H "statement to add the header file. At the same time, you must add dynamic connection import and export to winmm during compilation. lib. The specific implementation method is to select Settings from the project menu of developer studio, and then add winmm to the object/library modules control on the Link tab. lib. 2) You can specify different data in pwaveouthdr. lpdata to play the sound at any specified position in the audio data file. 3) All the above programs have passed the debugging in VC ++ 6.0, and the handling of errors and exceptions is omitted in this article, which must be added to practical applications.
Iv. Conclusion
In VC ++, you can use different methods to play audio files as needed. Simple Applications can directly call the sound playback function. The second method adds sound to an executable file as a resource. If you want to process audio data before playing the video, you can use the third method.

Bibliography:
1. American Paul Perry Chen xiangqun translated Multimedia Development Guide by Tsinghua University Press
2. Us Peter Norton, Rob McGregor sun fengying translated "MFC development Windows95/NT4 application program" by Tsinghua University Press 1998
3. Zhou jingli multimedia sound card technology and application, Electronic Industry Press 1998

1. Advanced Audio functions.
Messagebeep, playsound, and sndplaysound. You should be familiar with these three functions.
Too many. Because I feel very simple, you can check msdn. Pay attention to what header files they need,
What are the functions of parameters and what file formats are limited? For example, neither of them can play waveforms larger than kb.
Audio file.
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 at the same time.
Once it is used, the interface of the device is occupied, and the second time it is used, or the interface is snatched from the first
Once you can't use it, or you can't take it away.
2. MCI
What is MCI?
The media control interface is a set of multimedia settings provided by Mircrosoft.
The standard interface for backup and file, which has the advantage of being able to conveniently control the vast majority of multimedia devices, including audio, video, and video
Multimedia devices, such as discs and videos, do not need to know their internal working conditions.
Although Xiao He. MCI seems to be an advanced solution, 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. programs do not have to care about specific devices,
This allows you to use a laser player (CD), video player, waveform audio device, video player, MIDI device, and other media devices.
For programmers, they can regard the MCI as a row of buttons on the device panel, by selecting different
Buttons (send different MCI commands) allow devices to complete various functions without worrying about the internal implementation of the device. For example,
For play, the video disc machine and the CD machine have different responses (one is to play a video and the other is to play audio ).
However, you only need to press the same button.
Playback of audio files in VC
Playing audio file with Visual C ++ 3
Usage of MCI
The application sends a command to the MCI to control the media device. The interface of the MCI command is divided into command strings and command messages.
Command string is easy to use, but its execution efficiency is not as efficient
Message. (mcisendstring, 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 of the following parameters
They are all made up of 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 ",...);
The above example is written randomly. You should suddenly feel blue sky on your head.
Well, what does repeat mean? You really like the song. You have to listen to it once.
Let's talk about it. First, send a command, put the mouse into the device, and then send it again.
Send a command device to start running for me!
All MCI command strings are passed to MCI through the multimedia API function mcisendstring.
Number statement:
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, the error code is returned.
// You can just take a look at the following items. For error handling, you don't have to worry about writing a program. If you use it, just copy it.
Okay.
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
Playback of audio files in VC
Playing audio file with Visual C ++ 4
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 two songs.
Start playing, then I will add a mcisendstring ("Open rice love mouse. Avi type device 1
",...); No
This is not acceptable.
In this case, you need to create a different alias for the device that is opened each time, so that the MCI can differentiate the two playback settings.
Backup.
For example, the following section Code Opened and played 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, such as opening a .wav file, as long as the following two blue parts
Playback of audio files in VC
Playing audio file with Visual C ++ 5
For
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 them. For example
# Include;
# Include
# Include
# Pragma comment (Lib, "vfw32.lib ")
# Pragma comment (Lib, "winmm. lib ")
When I use it, 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 one is used.
Function, check msdn, and then put the header file that needs it.
In addition, I feel that many strong online players use the following methods to play audio files: mciwndcreate and
Mciwndplay. Of course, all of them use the MCI class. This is really convenient and simple.
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, filenam
E );
Mciwndplay (m_hwndmci );
Add the following to stdafx. h:
# Include
# Pragma comment (Lib, "vfw32.lib ")
For more control functions, open the VFW. h file.
Playback of audio files in VC
Playing audio file with Visual C ++ 6
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

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.