PlaySound() blues

Aug 15, 2008 at 11:42pm
PlaySound(TEXT("laser.wav"), NULL, SND_FILENAME);
I tried this, but, unfortunately, I got a linker error. Any ideas?
Aug 15, 2008 at 11:43pm
You need to link with winmm.lib when you compile. Good luck!
Aug 15, 2008 at 11:44pm
How would I do that from an IDE?
Aug 15, 2008 at 11:50pm
It depends on your IDE. Generally there is a "project options" menu item somewhere that allows you to specify what libraries to link when compiling.

In VC++, you can use the comment pragma:
#pragma comment( lib, "winmm" )

I don't know about other IDEs.

[edit] fixed error
Last edited on Aug 15, 2008 at 11:52pm
Aug 16, 2008 at 1:08am
Now, it will compile (needed libwinmm.a), but there is no sound outputted. Any ideas?
I also tried sndPlaySound(), but not working.
Last edited on Aug 16, 2008 at 1:13am
Aug 16, 2008 at 1:23am
You sure you've got everything hooked up correctly and it is not muted?
Can other applications play sounds?

[Sorry, I've got to ask. I did the same thing once before. It took me a week to figure out that had I told Windows never to allow any sounds at all...]
Last edited on Aug 16, 2008 at 1:25am
Aug 16, 2008 at 1:28am
Yes.
Wait, what did you do to tell windows to not allow sound?
Last edited on Aug 16, 2008 at 2:07am
Aug 16, 2008 at 3:35am
Oy, that was a long time ago. I don't remember. I think it was somewhere in the sound configuration options from the Control Panel...

The only option you are specifying is SND_FILENAME, and the function is not returning FALSE?
1
2
3
4
5
6
7
8
9
10
11
if (!PlaySound(
       TEXT("lazer.wav"),
       NULL,
       SND_FILENAME | SND_ASYNC | SND_NODEFAULT 
       ))
  MessageBox(
    NULL,
    TEXT("Could not play \"lazer.wav\""),
    TEXT("fooey!"),
    MB_OK | MB_ICONINFORMATION | MB_TASKMODAL
    );

(The SND_ASYNC and SND_NODEFAULT flags are optional for this test...)

Oh, and the "lazer.wav" file is in the current directory, the windows directory, the windows system directory, one of the directories listed in the PATH environment variable, or a network-mapped directory?
Last edited on Aug 16, 2008 at 3:36am
Aug 16, 2008 at 4:02am
Hey QWERTYman just a moment ago i didn't even know that we could put sound in c++ programs especially in console ones.

I saw your post and searched and found that you could. I looked up other forums and found how to play a sound file and i succcesfully played it.

Here's how i did it :
1. Make sure you are using Windows 98 and above.
2. Include the necessary librares and header files (windows.h and winmm)
3. I used the same syntax as you posted (i just did ctrl+c and ctrl+v :P)
4. I made sure that the music file was under my working directory

And BINGO my fav Halo Music in my console Project !!
Aug 16, 2008 at 10:18am
Interestingly, the Beep() function does not work either.
Aug 16, 2008 at 11:20am
If you are not finding winmm.h then include mmsystem.h
make sure you have winmm.lib in your project library path
1
2
3
4
5
6
7
8
#include <windows.h>
#include <mmsystem.h>

int main(){
PlaySound("mostreliable.wav", NULL, SND_FILENAME);
    return(0);
return 0;
}
Aug 16, 2008 at 11:22am
Interestingly, the Beep() function does not work either.

Hmm... that is indicative of something weird.
If you open the Windows Explorer and double-click the wav file can you hear it?
Last edited on Aug 16, 2008 at 11:23am
Aug 16, 2008 at 11:46am
Yes...
Creepy.
Aug 16, 2008 at 3:47pm
Maybe one of the recent windows updates did it? My system just did the same thing to me. I rebooted and everything works fine now...

Can you get the following to compile and play?
wave.cc
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
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <limits>
#include <string>
#include <windows.h>

void play_wave_resource( std::string resname, bool is_loop ) {
  HANDLE hfound, hsound;
  LPVOID data;

  hfound = FindResource( NULL, resname.c_str(), "WAVE" );
  if (hfound != 0) {

    hsound = LoadResource( NULL, (HRSRC)hfound );
    if (hsound != 0) {
      data = LockResource( hsound );

      if (data != NULL) sndPlaySound(
        (char *)data,
        SND_ASYNC | SND_MEMORY | (is_loop ? SND_LOOP : 0)
        );

      UnlockResource( hsound );
      }

    }
  FreeResource( hfound );
  }

void stop_wave_sound() {
  sndPlaySound( NULL, 0 );
  }

int main() {
  using namespace std;

  play_wave_resource( "one", true );

  cout << "Press ENTER";
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  stop_wave_sound();

  return EXIT_SUCCESS;
  }

res.rc
1
2
one WAVE "c:\\WINDOWS\\Media\\notify.wav"

Makefile
1
2
3
4
5
6
wave: wave.cc res.o
	g++ -o wave wave.cc res.o -lwinmm

res.o: res.rc
	windres -o res.o res.rc
	

(I used the GCC.)
Aug 16, 2008 at 6:30pm
No.
Aug 16, 2008 at 6:52pm
Alas, I'm out of ideas. I don't know what is wrong with your system... Sorry.
Topic archived. No new replies allowed.