Reputation: 230
Windows 7 Ultimate 64Bit, I am using CodeBlocks 16.01. I am working on my OpenGL college project, I'm trying to add a gun sound effect when the player shoots using the 'spacebar' key.
The sound only works when i create a console project or empty project and the sound plays just fine, I am aware of the library "winmm.lib" that i have to link, and it works just fine as a CONSOLE PROJECT, here is the code for this.
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
int main()
{
PlaySound("shotgun.wav",NULL,SND_SYNC);
return 0;
}
The problem begins when i now use this code in my Glut Project, what happens is instead of playing "shotgun.wav" it plays one of the windows system sounds called "Default Beep", When the shooting button "spacebar" is pressed. The game was freezing a few seconds until the sound has completed, however i fixed this issue by replacing thw above line with this one :
PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
It doesn't freeze anymore, But Still, It continues to play that Windows System sound instead of the shotgun.wav. The .wav file is IN the project, i'm pretty sure it's in the right place. here is the sample for shoot code from my Glut project (i only copied the relevant piece of code) :
void Keys(unsigned char key, int x, int y) {
key = tolower(key); //Just in case CAPS LOCK is ON.
if(key==27)
exit(0); //Escape key, Exit the game
switch(key){
case ' ': //SPACE BAR
/* some code here */
PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
break; //.....Code continues to the next case....
To force the gun sound effect, i had to go to Windows Sound settings and "Change System sounds", lol yep i actually had to replace "Default Beep" with my "Shotgun.wav", i then returned to the game and compiled, and wala ! It was working. BUT of course this is NOT what i want, cause obviously this will only work on my PC and we don't want that.
Upvotes: 0
Views: 2158
Reputation: 11
You need to specify the full file path. Use double slash \\
in between. For example:
sndPlaySound("D:\\New folder(2)\\poiuy\\sound.wav",SND_ASYNC|SND_LOOP);
Upvotes: 1