Hi,

I am trying to a play a .wav file in a C++ program.

I think my code is OK, but when it plays, it plays the default windows 'error' sound, not my .wav file.

This is the program:

#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
using namespace std;

int main()
{
	for( int a = 0;a<10;a++)
	{
		PlaySound((LPCWSTR)"tone.wav", 0, SND_LOOP|SND_ASYNC);
		Sleep(1000);
	}
	return 0;
}

How can i add the .wav file so it is found?

In the Visual Studio 2005 soulution explorer I right click on Resource Files>Add>Resource, but can't find an option for .wav.

Thanks for any help!

You forgot the SND_FILENAME argument, change that line to:

PlaySound((LPCWSTR)"tone.wav", 0, SND_LOOP|SND_ASYNC|[B]SND_FILENAME[/B]);

and it should work.

commented: Thanks for the help :) +1

Thanks William!

I changed the line, but it still makes the same 'error/not found' sound.

I honestly think it doesn't now where to find the .wav file?

Its a 26 KB .wav file.

I need to add the .wav file to the project some how. It's on my Desktop right now.

Well, to use that line of code, the .wav file has to be in the same directory as the executable/project folder if you're running directly from Visual Studio. If you want to attach it to your resouce file, that's easy too.

Yes, i just want to add it as a resource, so it plays.

I can see an option for .bmp files, but nothing for .wav.

See example code and attachments.

#include <windows.h>
#include "resource.h"
#pragma comment(lib,"Winmm.lib")

int WINAPI WinMain(
      HINSTANCE hInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpCmdLine,
      int nShowCmd)
{
  PlaySound( (char*)IDR_WAVE1, NULL, SND_RESOURCE | SND_ASYNC ); 
  MessageBox( NULL, "", "", MB_OK );
  return 0;
}

Thanks buddy :)

I got it to work, copy and pasted the .wav file into the Project's directory.

Used this code:

#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
using namespace std;

int main()
{
	for( int a = 0;a<1;a++)
	{
		PlaySound(L"tone.wav", NULL, SND_FILENAME | SND_SYNC);
		Sleep(3000);
	}
	return 0;
}

and now it plays.

Shame though that I can't see the file in the Solution Explorer.

I tried your recommendation and it created a .rc file, but was trying to convert char* into ____,

The file doesn't need to appear in the solution explorer. Do you know how to work with resource files? You can put any kind of file into a resource file, you just need the right commands to read them.

So, first you need to create a resource file for your project by right clicking on your project name and adding a resource file.

Then double click on your resource file in the solution explorer to open it, then right click on resource.rc and click "Add resource", then press 'Import'.

If you do all those steps right, it will work.

Hope this helps.

That's sweet.

Thanks ever so much for the help. I understand it now. yes it was the first time I'd ever tried to add a resource.

Real appreciated!

Thanks again :)

commented: You're welcome (: +5

Might be worth noting the Micorosft example uses MAKEINTRESOURCE instead of casting to a char pointer.

PlaySound(
MAKEINTRESOURCE(IDR_WAVE1),
GetModuleHandle(NULL),
SND_RESOURCE);

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.