PlaySound function

So... I want to use thw PlaySound(); but msdn didn't help a lot... Can anyone help me?
The MSDN page gives numerous examples of how to use it.

Here's one copy/pasted:

 
PlaySound(TEXT("recycle.wav"), NULL, SND_FILENAME);


Just replace "recycle.wav" with whatever filename you want to play.
remember the PlaySound() only read WAV\WAVE files. and you can use resource files too ;)

- Add the following entry to the resource script (.rc) file of the application.

soundName WAVE \sounds\bells.wav //i belive you can use double quotions for use the file name

- now heres how play the sound
PlaySound (TEXT("soundName"), NULL, SND_RESOURCE | SND_ASYNC);
I downloaded a wav file named cartoon182 and saved it in the same directory as the project but still it doesn't work... it says "undefined reference to 'PlaySoundA@12' ". Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
           case WM_CREATE:
                {
                          CreateWindow(TEXT("BUTTON"), TEXT("PLAY"),
                                   WS_CHILD | WS_VISIBLE | WS_BORDER,
                                   10,10,80,30,
                                   hwnd, (HMENU) 1, NULL, NULL                          
                          );
                          break;
                }
           
            case WM_COMMAND:
                {
                       if(LOWORD(wParam)==1)
                          {
                              PlaySound(TEXT("cartoon182.wav"), NULL, SND_FILENAME);
                          }
                          break;
                }
           
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Last edited on
it says "undefined reference to 'PlaySoundA@12' ".


You'll need to link to the winmm library.

If you are using Visual Studio:
Either go in your project's Linker settings and add "winmm.lib" to the list of libs you need

Or add this line to your program:
#pragma comment(lib,"winmm.lib")


If you are using another IDE
Go in your project's linker settings and add "winmm" to the list of libs you need.
I am using Dev C++! But still it doesn't work... :/
1st - by experience(yah... i started with Dev C++), give up on Dev C++ and use the Code Blocks with GNU compiler(it's a free C\C++ compiler. you must know some things for start the 1st compilation, but it's easy ;)

see if these video: https://www.youtube.com/watch?v=blZNVKYxyIY (youtube have very things ;) )
maybe these helps you more ;)
Last edited on
Make sure you have Orwell DevC++
http://orwelldevcpp.blogspot.com/
(Earlier versions are obsolete)

Go to the menu, Tools->Compiler Options
in the "General" tab, check the box marked "Add the following commands when calling the linker" and add -lwinmm in the area below.

Example:
1
2
3
4
5
6
7
8
#include <windows.h>

int main()
{
    
    PlaySound(TEXT("C:\\WINDOWS\\Media\\tada.wav"), NULL, SND_FILENAME);

} 
Topic archived. No new replies allowed.