Making A .WAV From A Directory Play

I have been researching and researching and I really don't understand...

C:\\Users\adryl\\Documents\\Visual Studio 2015\\Ding.wav

Here is my directory. I want to use the PlaySound() function. Please, someone just post exactly how, in one line or more, to play that sound from that same directory.
Last edited on
PlaySound( "C:\\Users\adryl\\Documents\\Visual Studio 2015\\Ding.wav", NULL, SND_FILENAME );
A bit of Googling would've easily told you how to use PlaySound().
https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx
Also, you need to include the windows media header as it's not included by Windows.h.

#include<mmsystem.h>
Also, you need to include the windows media header as it's not included by Windows.h.

Not correct.
Header Mmsystem.h (include Windows.h)

https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx

What you need to add is Winmm.lib since it is not included in VS projects.
Current Code: Got almost all of it working, thanks, there is still a problem with the directory. All of it is good except this error:

argument of type "const char *" is incompatible with parameter of type "LPCWSTR"



1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;

int main() {
	PlaySound("C:\\Users\\adryl\\Documents\\Visual Studio 2015\\Ding.wav", NULL, SND_FILENAME);

	return 0;

}
Last edited on
A few ways to fix it:

PlaySoundA("C:\\Users\\adryl\\Documents\\Visual Studio 2015\\Ding.wav", NULL, SND_FILENAME);

OR

PlaySound(L"C:\\Users\\adryl\\Documents\\Visual Studio 2015\\Ding.wav", NULL, SND_FILENAME);

OR

#include <tchar.h>
PlaySound(_T("C:\\Users\\adryl\\Documents\\Visual Studio 2015\\Ding.wav"), NULL, SND_FILENAME);
Sadly all three options failed. I did, however, manage to put the sound in "Resource Files", is there a way I can play it from there?
Why did it fail?
Are you sure that the file is in the right location and has the right format?
Also it's important to check the return value and call GetLastError(), in most cases that shows what the problem is.
Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <Windows.h>
#include <iostream>
#include <string>

#pragma comment(lib, "winmm.lib")

using namespace std;

string GetWindowsDirectoryA()
{
  string buffer(255, ' ');

  UINT len = GetWindowsDirectoryA(&buffer[0], 255);
  buffer.resize(len);

  return buffer;
}

int main()
{
  string filename = GetWindowsDirectoryA() + "\\Media\\tada.wav";

  PlaySoundA(filename.c_str(), 0, SND_FILENAME);

  system("pause");
  return 0;
}
Topic archived. No new replies allowed.