1

I'm currently working on a project and I need to play a sound file (which is actually a video but I only want to play the sound). Someone told me I could use a PlaySound function but it's currently not working (it plays this sound when I run instead of the file I wanted https://dl.dropboxusercontent.com/u/45453297/Windows%20Background.wav)

int _tmain(int argc, _TCHAR* argv[])
{
    PlaySound(TEXT("C:\\Users\\Ricardo\\Documents\\Visual Studio 2013\\Projects\\SomEVideo\\SomEVideo\\KinectAudio-02-23-32.wav"), NULL, SND_FILENAME);
    return 0;
}

On stdafx.h I included these headers:

#include "windows.h"
#include "mmsystem.h"

I also linked winmm.lib on my project so I don't get no unresolved problems.

Thanks.

EDIT:

After been told that the path was wrong, I changed the file name to "t.wav" and placed it in the hard drive root to avoid typing errors. I also tried PlaySounDW and play it like a resource. Here's what I'v got so Far with no success:

int _tmain(int argc, _TCHAR* argv[])
{
    PlaySoundW(TEXT("C:/t.wav"), NULL, SND_FILENAME); //GetLastError returns 0
    PlaySoundW(TEXT("C:\\t.wav"), NULL, SND_FILENAME); //GetLastError returns 0
    PlaySoundW(TEXT("t.wav"), NULL, SND_FILENAME); //GetLastError returns 0

    PlaySound(TEXT("C:/t.wav"), NULL, SND_FILENAME); //GetLastError returns 0
    PlaySound(TEXT("C:\\t.wav"), NULL, SND_FILENAME); //GetLastError returns 0
    PlaySound(TEXT("t.wav"), NULL, SND_FILENAME); //GetLastError returns 0

    PlaySoundW(MAKEINTRESOURCE("C:/t.wav"), GetModuleHandle(NULL), SND_RESOURCE); //GetLastError returns 1813
    PlaySoundW(MAKEINTRESOURCE("C:\\t.wav"), GetModuleHandle(NULL), SND_RESOURCE); //GetLastError returns 1813
    PlaySoundW(MAKEINTRESOURCE("t.wav"), GetModuleHandle(NULL), SND_RESOURCE); //GetLastError returns 1813

    PlaySound(MAKEINTRESOURCE("C:/t.wav"), GetModuleHandle(NULL), SND_RESOURCE); //GetLastError returns 1813
    PlaySound(MAKEINTRESOURCE("C:\\t.wav"), GetModuleHandle(NULL), SND_RESOURCE); //GetLastError returns 1813
    PlaySound(MAKEINTRESOURCE("t.wav"), GetModuleHandle(NULL), SND_RESOURCE); //GetLastError returns 1813

    return 0;
}
Ricardo Alves
  • 1,071
  • 18
  • 36
  • Ricardo - can you share the "KinectAudio-02-23-32.wav" file you are trying to play? I suspect it is not a proper WAV file since you mentioned `is actually a video`. – selbie Jul 09 '14 at 16:25
  • 1
    It is a WAV file. I wanted to play the audio of a video but I started with something more simple (WAV file). Here's the wav file: https://dl.dropboxusercontent.com/u/45453297/KinectAudio-02-23-32.wav – Ricardo Alves Jul 10 '14 at 16:35
  • 1
    Ricardo - your code is fine. Your WAV file itself is the problem. The format chunk header is a format most PC audio drivers are not able to play. I inspected it with a hex editor. It's 4 channels of audio, 16khz, 256000 bytes/sec, 32 bits per sample. Unless you have a pro-audio soundcard, that won't open on conventional PC hardware without a transcoding step. Most media players can't play that file. Windows Media Player plays it only because it transcodes it to 2-channel 16-bit. – selbie Jul 10 '14 at 17:53

2 Answers2

0

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

Check your path.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

which is actually a video but I only want to play the sound

You can't do this. If the file is a video (including AVI) then it is not a WAV file, even if you change the file extension. PlaySound only knows how to decode and play files of type RIFF with a WAVE chunk.

PlaySound can however play WAV files where the samples are compressed with a codec.

You can use a tool such as FFMPEG to extract the audio segment from the video file and save it to a standalone WAV file.

Another solution might be to use one of the bigger APIs (DirectShow or MediaFoundation) to play the video file without a window to show the video. This might be a lot of code and working set for your needs.

selbie
  • 100,020
  • 15
  • 103
  • 173