Link to home
Start Free TrialLog in
Avatar of PudBawl
PudBawl

asked on

audio in win32 c++ consol app

I was given this line of code to play .wav files from my .cpp file.

#include <windows.h>
#include <mmsystem.h>
//the winmm.lib library is also added to the project

void main()
{//begin main function

PlaySound("C:\ric1.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);

}//end main function

As it is Microsoft Visual Studio 6.0 will compile the code but no sound is played, it merely opens the consol window and ends the program.  I need something that will play a .wav file with as little code as possible.
ASKER CERTIFIED SOLUTION
Avatar of sasanj
sasanj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PudBawl
PudBawl

ASKER

Apparently it can find the file, modifying the path doesn't change the results any
Take a look at this extract of MSDN library about playsound:

"The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver. PlaySound searches the following directories for sound files: the current directory; the Windows directory; the Windows system directory; directories listed in the PATH environment variable; and the list of directories mapped in a network. For more information about the directory search order, see the documentation for the OpenFile function.

If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE."

in addition check the return value of playsound.

hope it helps.

PlaySound with the SND_ASYNC flag return immediately and your app is exiting before it starts.  Removing the SND_LOOP|SND_ASYNC would play it once, otherwise you must do something like this.

//------------------------------------------

#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>

void main()
{

PlaySound ("C:\\sound.wav", NULL, SND_FILENAME|SND_LOOP|SND_ASYNC) ;
getchar () ;

}

//-------------------------------------------


Avatar of PudBawl

ASKER

I've attempted changing the location of the file, and the path, neither worked but I've found that the function is indeed returning FALSE.  I also tried removing the extra parameters |SND_LOOP|SND_ASYNC but had no success because the function is returning FALSE.
did you type "C:\\..."  or "C:\..."? you have to use \\ if you want a backslash to be in the string!!!
Avatar of PudBawl

ASKER

yes
hi,
Why don't you remove the path, and put the file in default path that windows will look in?
try this option, look at my previous comment for pathes!
Avatar of PudBawl

ASKER

Turns out I had two problems, a more complex part of my program had corrupted the .wav file so it was no longer playable, but I also was missing a "\" as sasonj had originally commented.  Thank you all for your help.
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days.
Unless there is objection or further activity,  I will suggest to accept "sansanj" comment(s) as an answer.

The link to the Community Support area is: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please do not accept this comment as an answer!
======
Werner
Force accepted

** Mindphaser - Community Support Moderator **