Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to C++ ...need help

Status
Not open for further replies.

iasonus

Programmer
Oct 16, 2000
9
SE
Hi

Very simple one this. I'm using VC++ 6.0, with MFC. JUst need to know the easiest play to get a .wav to play.

Many thanks
jason
 
You can use PlaySound() API

if (!PlaySound("C:\\WINDOWS\\YOUR_FULL_PATH_HERE\\SONG.WAV", 0, SND_FILENAME))
{
cerr<<"Error in reading the file"<<endl;
}

If you need greater control, you should consider using DirectX's DrectSound API.

Please consult MSDN for further information. I hope it helps.

Zech
 
Hi

Thanks Zech. I'm new to C++, but I read somewhere that is was easier to do with:

sndPlaySound()

Can you show me a method using this, and tell me what stuff to #include?

Many thanks
Jason

 
MSDN quotation:
The PlaySound Function
You can use the PlaySound function to play waveform audio, as long as the sound fits into available memory. (The sndPlaySound function offers a subset of the capabilities of PlaySound. To maximize the portability of your Win32-based application, use PlaySound, not sndPlaySound.)
 
I haven't really tested this but it should work. The full description of the API can be found here
#include <Windows>

using namespace std;

void main ()
{
//read the msdn for the full description of the flags
if(!sndPlaySound("C:\\FULL_PATH_HERE\\YOUR_SONG.WAV", SND_NODEFAULT||SND_NOSTOP||SND_SYNC))

cerr<<"Error in reading the file."<<endl;

return;
}
 
Thanks. I got the documentation on PlaySound() and sndPlaySound(), but whatever I try, it won't compile, and I get this message "Cannot open precompiled header file".

 
Yup. I added it as a module in the Project settings box.
 
I overlooked certain things.

#include <Windows.h> //You need to add .h otherwise VC will complain
#include <iostream> //for cerr

using namespace std; //for cerr

void main()
{
if(!sndPlaySound("C:\\FULL_PATH\\SONG.wav", SND_NOSTOP))

cerr<<"Error in reading the file."<<endl;

return;

}

In addition, you need to go to Project->Setting->Link and make sure that Winmm.lib is added into your library module. If not, add it.
 
I´keep getting these errors:

error C2065: 'PlaySound' : undeclared identifier
error C2065: 'SND_ASYNC' : undeclared identifier
 
1. Make sure that you have included Windows.h
2. Link the Winmm.lib
3. Make sure that you have the correct syntax there. Assuming the compiler error message is accurate in determining your error (which sometimes it doesn't), it seems that your PlaySound() is treated as a variable instead of a function. Please paste the relevant part of your code if you still can't figure out what's wrong with your codes.
 
Hi

I redid the whole project and it worked just fine. So hard to spot the bugs / typos.

Thanks all for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top