Link to home
Start Free TrialLog in
Avatar of Hobbes7714
Hobbes7714

asked on

PlaySound in VC++ 6.0

I've read over everything I can find on the net (and on this site) about "PlaySound", and I can't get mine to work.  I've included mmsystem.h, and added winmm.lib to my project (both debug and release).  When I execute the file, it doesn't give me an error (it returns true), but doesn't play anything.  I've made sure that the file is in the correct directory (many times ;)), and I know that the file contains good sound data (by playing it with winamp).

The code is:
if (!PlaySound ("C_Major.wav", NULL, SND_ASYNC)) exit (1);

I've tried many different variations of that, but none of them work, and it doesn't exit.  Has anyone had any problems like this, or have any insight?  Or even any other (decently simple) methods of playing a wav file?
Avatar of PlanetCpp
PlanetCpp

you're not giving it a chance to play probably. im guessing you exit right after that PlaySound code, or at least you dont wait for anything. change SND_ASYNC to SND_SYNC
SND_ASYNC will return right after it starts the sound, your program has to wait. if the program ends before the sounds even starts to play,,it doesn't play
Avatar of Hobbes7714

ASKER

I've tried using SND_SYNC and that didn't work either.  After that line of code I actually have a whole bunch of 3D stuff, the amount of time before the program quits is greater than the length of the file.
Avatar of Neeraj Soni
try this as flag:
SND_NOWAIT + SND_NODEFAULT + SND_SYNC
you definitely need SND_SYNC in your case -- but, I have encountered WAV files that playsound won't play also.  
Unfortunately, it's been so long since I've done anything with audio that I can't remember right now what the case was.  

Try using a wav editor and make sure you save your WAV file as something basic like 16kbps and 16kHz stereo.

Winamp uses an entirely different set of libraries to play audio, so winamp can play files that PlaySound can't handle.

ASKER CERTIFIED SOLUTION
Avatar of PlanetCpp
PlanetCpp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I've made sure that the file is in the correct directory (many times ;)),

But perhaps you have not made sure that the system thinks it is in that directory ;)  Try it with a full drive, path, and filename:

BOOL fRet= PlaySound("c:\\Mysounds\\C_Major.wav", NULL, SND_FILENAME | SND_SYNC);

if ( !fRet ) {
    printf("oops!")
}

Also note that I added the SND_FILENAME flag as suggested by PlanetCPP

-- Dan
Hi,

I do not know what C++ compiler you are using if.. by any chance you are using DevC++ then you will haev to use the follwoing code..


#include <iostream>                    

// This line of code is necessary to include all the C++ standard functions\classes definitions

#include <windows.h>
// This needs to be included for PlaySound()

#include <mmsystem.h>
/* This also needs to be included for Playsound()
In all actuality, mmsystem.h is needed for PlaySound,
windows.h is required before mmsystem.h.  You'll also
notice if you go to project options (ALT-P) that
the winmm library (-lwinmm) is included in the project.
*/

/*
Note : I believe that mmsystem.h is included in windows.h
in a Visual C++ implementation.  However that is not the case in the mingw implementation, hence we have to include both files.
*/

using namespace std;

int main()                              
{                                        cout << "Playing synchronous sound..." << endl;
/*
The second option, "SND_SYNC" tell PlaySound that we want to play the sound synchronously. Playing a synchronous sound means that NOTHING will continue until the sound finished. In other words, that means that the program will not continue until the sound finishes.
*/

PlaySound("mysound.wav", NULL, SND_FILENAME | SND_SYNC);

Sleep(1500);          // This sleeps for 3 seconds

/*                                  
The above code will take the wav file, play the sound, then continue on once it is finished.
Use SND_ASYNC instead of SND_SYNC.
This would give us an asynchronous sound.  This means that the sound will play in the background.
*/

PlaySound("mysound2.wav", NULL, SND_FILENAME | SND_ASYNC);

/*
This message will be displayed while the above sound is playing
*/
     cout << "playing asynchronous sound";
                                       
                                        // Here we create a simple for loop to show that we are doing something while the sound is being played.
     for(int i = 0; i < 50; i++)
     {
          cout << ".";               // Print out a dot each iteration of the loop
          Sleep(50);                    // Pause the program for 1/20 of a second (1000 = 1 second)
     }

     cout << endl;                    // Position the cursor to the next line
    return 0;
}    

This should get Playsound working.... and if it does not work then you could multiple problems ...everything from "there is a DMA conflict" to "some other device using your resource". Make sure you are not running any application that is using the sound device.
yep
sndPlaySound is teh best

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_sndplaysound.asp

//Use SND_ASYNC if you want your code to continue and not wait for the sound to finish playing.
sndPlaySound( "Chime.wav", SND_ASYNC);

//Use SND_SYNC if you want the Wav to finish before the function returns
sndPlaySound("Chime.wav", SND_SYNC);

or

use OPEN AL to play more than onessound at once, and for really cool effects and depth and sound decreaseing in loudness and pitch when it gets far away ect. goto www.openal.com
oops sorry, wrong address www.openal.org/
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to PlanetCpp: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (https://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer