Because you only need to execute it on Windows, first think about using the MCI interface. After a try, mcisendcommand can be used to play the basic WAV file. However, loop playback of Wav is troublesome. The message mm_mcinotify must be sent to the window.
After Google, we found that there was a simpler method-UseSndplaysound. One statement, sndplaysound (filename, snd_async | snd_loop), can play audio files cyclically, fully meeting my requirements. Simple.
Function Definition:
Bool sndplaysound (lpcstr lpszsound, uint fusound);
Here, lpszsound is generally the name of the wav file, and fusound is the parameter. Common fusound parameters include:
Snd_async asynchronous playback, that isProgramResume the operation after the playback ends. The background sound is played.
Snd_sync: Synchronous playback.
Snd_loop loop playback
If the specified file cannot be found, keep it quiet. If this parameter is not specified, the default warning sound of the playing system is used. If there is no default warning sound, it is a failure.
If the execution is successful, true is returned. If the execution fails, false is returned.
To stop playing, you only need to execute the sndplaysound function with the lpszsound parameter null again.
Requirements:
The program must be added to the mmsystem. h file and linked to the winmm. Lib library during compilation.
Restrictions:
Sndplaysound can only play wav files.
The WAV file will be loaded into the memory before playback, so it cannot be too large.
Only one sound can be played simultaneously. The last sound will close the previous sound.
Function playsound is an enhanced version of sndplaysound. It supports more sound types and fusound parameters, and can play back sounds in memory and resources.
Or Add the following:
# Include <mmsystem. h>
# Pragma comment (Lib, "winmm. lib ")
Playsound (...)
The first method is to directly broadcast the sound file.CodeIs:
Playsound ("C: \ Win95 \ media \ The Microsoft sound.wav", null, snd_filename | snd_async );
Note that the path in the parameter uses two consecutive backslash escape to represent a backslash.
The second method is to add the sound file to the resource and then play the sound from the resource. Visual c ++ supports wave resources. You can right-click the resource view and select the import command. Then, in the file selection dialog box, select the Microsoft sound.wav file, the file will be added to the wave resource. Assuming that the sound resource ID is idr_startwin, the following call also outputs the Startup sound:
Playsound (lpctstr) idr_startwin, AfxGetInstanceHandle (), snd_resource | snd_async );
The third method is to use playsound to play the system sound. The Windows Startup Sound is defined by systemstart. Therefore, you can use the following method to play the Startup sound:
Playsound ("systemstart", null, snd_alias | snd_async );
Sndplaysound cannot directly play audio resources. To use this function to play the wave file, you can call the following method:
Sndplaysound ("mysound. wav", snd_async );