play sound

I managed to use playsound in visual studio and I used a code which I found in an old forum the program only run when I edit project properties =>linker=>input=>external dependencies and add winmm.lib but the only sound I get is an error sound have I made something wrong in the setting or do I have to put the wav file in a specific place or do I have to change the code

1
2
3
4
5
6
7
8
9
10
11
 #include<iostream>
#include<windows.h>
#include<mmsystem.h>
#include<stdlib.h>
using namespace std;
int main(char argc)
{
	PlaySound(TEXT("C:\\mariam.wav"), NULL, SND_FILENAME | SND_ASYNC); 
	system("pause");
	return 0;
}

thanks in advance for any help=D
Last edited on
Change C:\\mariam.wav to C:/mariam.wav so you don't have to worry about backslashes.

Is there definitely a file named mariam.wav at that location? Right at the root of the C: drive?
Your code looks ok. Probably the filename is wrong.
If the function cannot find the specified sound and the SND_NODEFAULT flag is not specified, PlaySound uses the default system event sound instead.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680%28v=vs.85%29.aspx

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
28
29
30
31
32
#if !defined(UNICODE) || !defined(_UNICODE)
#define _UNICODE
#endif

#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#include <string>

bool FileExists (std::wstring& filename)
{
  WIN32_FIND_DATA fd = { 0 };

  HANDLE hFile = FindFirstFileW (filename.c_str (), &fd);

  return hFile != INVALID_HANDLE_VALUE;
}

int main (char argc)
{
  std::wstring filename (L"C:\\mariam.wav");

  if (FileExists (filename))
    PlaySound (filename.c_str(), NULL, SND_FILENAME | SND_ASYNC);
  else
    std::wcout << L"Filename " << filename.c_str () << L"does not exist.";

  std::wcout << L"\n\n";
  system ("pause");
  return 0;
}
I tried changing c:\\ to c:/ but it didn't work
when I tried the code and the console output was that he didn't find the file
I don't know I'm pretty sure this file exists is there a specific place I should put it in
I have more than one file with the same name on my pc may this cause an error?
You need to find where the file is, and provide that information. It doesn't matter where you put it, but you have to tell your program where it is.


I have more than one file with the same name on my pc may this cause an error?
No
Last edited on
I wrote the full path
the console output was two empty lines I guess this means he found the file but the sound didn't change it gives an error sound :(
Is the file a valid wav file? Can you play the file using a sound player?
yes I played it with a sound player and there is nothing wrong
I recorded a file and changed its extension to a wav file by renaming it

Changing the extension to "wav" doesn't make it a wav file. You need an actual wav file.
Add this to the else clause. std::wcout << L"Windows error: " << GetLastError ();

Try to move the file to a different folder. I vaguely remember a similar problem here in the forum where the problem was no access to files in the root directory.

I downloaded a wav file and it worked =D
sorry for this stupid mistake and thanks very much for help=D
Topic archived. No new replies allowed.