C++ WinAPI PlaySound example
- User
C++ WinAPI PlaySound example
- Assistant
Here's an example of how to use PlaySound function in C++ WinAPI:
#include <Windows.h> #include <mmsystem.h> #pragma comment(lib, "winmm.lib") int main() { PlaySound(TEXT("sound.wav"), NULL, SND_FILENAME); return 0; }This example demonstrates how to play a sound file named "sound.wav" using the PlaySound function in the WinAPI. First, we include the necessary headers for Windows.h and mmsystem.h.
Then, we use the
PlaySoundfunction, which takes three parameters: the first parameter is the name of the sound file to be played ("sound.wav"in this case), the second parameter is a handle to the sound resource (NULL here), and the third parameter is a set of flags that control how the sound is played (SND_FILENAME flag specifies that the first parameter is a file name).Finally, we return 0 to indicate that the program ran successfully.
To play a sound file, you need to link against the winmm.lib library by adding the
#pragma comment(lib, "winmm.lib")directive in your code.Make sure to replace
"sound.wav"with the actual sound file path that you want to play.