Ze WAVE sound file player

vegaseat 1 Tallied Votes 209 Views Share

Short little sound files of chicken, horses, dogs, cars, trains and other annoying things is the realm of the wave file (.wav). These are easy to play and guaranteed to confuse grandpapa. Again, we are using Windows' sound workhorse winmm.lib file. Ze function this time is PlaySound(). Here is ze C++ console code.

// play a sound with PlaySound() from the winmm.lib
// For DevCpp add the library libwinmm.a to the linker via 
// Project/Project Options/Parameters/Add Library
// a Dev-C++ tested console application by  vegaseat  19dec2004

#include <cstdio>      // getchar()
#include <windows.h>   // PlaySound()

#define SND_FILENAME 0x20000  // from mmsystem.h
#define SND_LOOP  8
#define SND_ASYNC 1
#define SND_ALIAS 0x10000


using namespace std;

int main()
{
  // pick a wave file supplied by Windows XP or one of your own ...
  char soundfile[] = "C:/Windows/Media/chimes.wav";
  
  // play sound as loop, warning - can be vexatious!!!
  PlaySound(soundfile,NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
  
  // without SND_LOOP it just plays it once
  //PlaySound(soundfile,NULL,SND_FILENAME|SND_ASYNC);
  
  // play the system exit sound if set
  //PlaySound("SystemExit",NULL,SND_ALIAS);   
  
  getchar();  // wait
  return 0;
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Replaced iostream header and cin.get() wait with cstdio header and getchar() wait to bring the exe file down in size from 415k to 15k.

Nauro 0 Newbie Poster

If you wanted to use the same code to play multiple wav files how would you go about it. The code I used is.
int main()
{

string soundstring = "Sound/.wav";
string scenecode = "0000";//Put 4 digit wav name here
soundstring.insert(6, scenecode);
char soundfile[] = soundstring; PlaySound(soundfile,NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
getchar();
return 0;
}

I would like to only need to change the 4 digit code to play a different wav file.

The error I'm getting is:
error:
char soundfile[] = soundstring;

initializer fails to determine size of "soundfile"

danibootstrap 13 Light Poster

Hey vegaseat,I was able to play the sound file (.wav) when i created a project.But now i require that i play the file just as a normal .cpp file (by not creating a project)How shall i do it?And siince i am not creting a project how shall i add the libwinmm.a to the file.Please tell me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.