A problem with PlaySound

Mar 26, 2016 at 12:55am
closed account (NUCkSL3A)
Hello, I am currently doing a project, and part of it requires a sound to be played. Here's the code fragment that's troubling me:

PlaySound(L"C:\\Windows\\Media\\Calm\\nuke.wav", NULL, SND_FILENAME);

I double checked that the file existed, and that I typed the name in right. But when the playsound command is run, it just makes a ding. Any help would be appreciated. Oh, and btw, this works with system wav files, but not with this one. (Yes, I did test that my wav is a valid file)
Last edited on Mar 26, 2016 at 1:17am
Mar 26, 2016 at 6:42am
From the MSDN PlaySound documentation:

If the file cannot be found, the function plays the default sound unless the SND_NODEFAULT flag is set.


The "ding" you're hearing is the default system event sound.

So, PlaySound cannot find the file because either:

1.) The file path is wrong
2.) The file doesn't exist
Last edited on Mar 26, 2016 at 6:42am
Mar 26, 2016 at 7:49pm
closed account (NUCkSL3A)
But the problem is that it DOES exist, and I typed in the path right. It works with system sounds but not any that I downloaded.
Mar 26, 2016 at 10:00pm
Try this code and see if you get any error msg.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <windows.h>
#include <tchar.h>
#include <conio.h> // for _tcprintf

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

bool FileExists (TCHAR* szFileName)
{
  DWORD result = 0L;
  WIN32_FIND_DATA fd;
  HANDLE hFile;

  hFile = FindFirstFile (szFileName, &fd);
  if (hFile == INVALID_HANDLE_VALUE)
  {
    return false;
  }
  FindClose (hFile);

  return true;
}

void ShowWinError ()
{
  TCHAR szBuf[1024];
  TCHAR *lpMsgBuf;
  DWORD err_code = GetLastError ();

  FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                 0, err_code, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
                (TCHAR*)&lpMsgBuf, 0, 0);

  _tcprintf (szBuf, _T("Error: %d: %s"), err_code, lpMsgBuf);

  MessageBox (NULL, szBuf, _T("Error"), MB_OK);

  LocalFree (lpMsgBuf);

}


int main ()
{
  TCHAR szFileName[] = _T ("C:\\Windows\\Media\\Calm\\nuke.wav");

  if (!FileExists (szFileName))
  {
    ::MessageBox (0, _T ("File does not exist"), _T ("MISSING FILE"), MB_OK | MB_ICONERROR);
    return EXIT_FAILURE;
  }
  if (!PlaySound (szFileName, 0, SND_FILENAME | SND_NODEFAULT))
  {
    ShowWinError ();
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
Mar 27, 2016 at 8:31pm
closed account (NUCkSL3A)
nope, that still didn't detect it.
Mar 27, 2016 at 10:22pm
Check your WIN.INI or system registry to make sure you haven't changed the sound association attached to that file.
Mar 30, 2016 at 12:10am
closed account (NUCkSL3A)
I found the problem. The guy who sent me the files didn't convert them to wav, just changed the file extension. Anyways, it's all cleared up.
Topic archived. No new replies allowed.