PlaySound in VC++ 6.0

Posted on 2003-03-04
Medium Priority
Last Modified: 2008-02-26
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?
Question by:Hobbes7714
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8069578
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

Author Comment

ID: 8069767
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.

Expert Comment

by:Neeraj Soni
ID: 8069834
try this as flag:
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8069968
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.


Accepted Solution

PlanetCpp earned 300 total points
ID: 8069984
i forgot if this is needed or not i think i always include it
LVL 49

Expert Comment

ID: 8070595
>>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 ) {

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

-- Dan

Expert Comment

ID: 8070991

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.

Expert Comment

ID: 9777067
sndPlaySound is teh best


//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);


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

Expert Comment

ID: 9777073
oops sorry, wrong address www.openal.org/
LVL 11

Expert Comment

ID: 9888145
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 (http://www.experts-exchange.com/help.jsp#hi73).


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question