943,548 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7617
  • C++ RSS
Jul 23rd, 2008
0

Visual C++ 2008 playing a sound

Expand Post »
Hi,

I need help playing a sound in Visual C++ 2008. (Windows Forms App). I've tried many things searching on google, I used this method:

C++ Syntax (Toggle Plain Text)
  1. PlaySound("beep.wav", 0, SND_LOOP|SND_ASYNC);

Then I get the error, "'PlaySoundW' : cannot convert parameter 1 from 'const char [9]' to 'LPCWSTR''. Can anyone help me play the sound 'beep.wav' that I have in my resources folder? I need it to loop forever, (or 9999999999999999999999 times if u know wut i mean ).

Thanks in advance to anyone who can help.
Last edited by Wiki_Tiki; Jul 23rd, 2008 at 6:38 pm.
Reputation Points: 23
Solved Threads: 3
Junior Poster in Training
Wiki_Tiki is offline Offline
60 posts
since Jul 2008
Jul 23rd, 2008
0

Re: Visual C++ 2008 playing a sound

It has to do with the way your compiler is interpreting strings (I think it happens when you set the character encoding to unicode instead of multibyte, or something like that).

I think that this:
C++ Syntax (Toggle Plain Text)
  1. PlaySound((LPCWSTR)"beep.wav", 0, SND_LOOP|SND_ASYNC);
will work.
Last edited by CoolGamer48; Jul 23rd, 2008 at 6:44 pm.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 23rd, 2008
0

Re: Visual C++ 2008 playing a sound

You could use the Beep command...

Beep(523,500); // 523 hertz (C5) for 500 milliseconds
Beep(587,500);
Beep(659,500);
Beep(698,500);
Beep(784,500);

but you probably didn't want to hear that from a novice like me...
Reputation Points: 10
Solved Threads: 1
Light Poster
Pikachumanson is offline Offline
45 posts
since May 2008
Jul 23rd, 2008
0

Re: Visual C++ 2008 playing a sound

Since you are compiling with the UNICODE flag, you must either indicate that string literals are wide-char directly: L"Hello world!" or (better yet) use the TEXT() macro:
PlaySound(TEXT("beep.wav"), 0, SND_LOOP|SND_ASYNC);

Have fun.
Last edited by Duoas; Jul 23rd, 2008 at 7:25 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jul 23rd, 2008
0

Re: Visual C++ 2008 playing a sound

I forgot to describe at the beginning that this is supposed to loop forever after a timer finishes. Anyway, I get the same 3 errors, using any of your snippets:

1. error LNK2028: unresolved token (0A000011) "extern "C" int __stdcall PlaySoundW(wchar_t const *,struct HINSTANCE__ *,unsigned long)" (?PlaySoundW@@$$J212YGHPB_WPAUHINSTANCE__@@K@Z) referenced in function "private: void __clrcall Timer1::Form1::timer1_Tick(class System::Object ^,class System::EventArgs ^)" (?timer1_Tick@Form1@Timer1@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) Timer.obj

2. error LNK2019: unresolved external symbol "extern "C" int __stdcall PlaySoundW(wchar_t const *,struct HINSTANCE__ *,unsigned long)" (?PlaySoundW@@$$J212YGHPB_WPAUHINSTANCE__@@K@Z) referenced in function "private: void __clrcall Timer1::Form1::timer1_Tick(class System::Object ^,class System::EventArgs ^)" (?timer1_Tick@Form1@Timer1@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) Timer.obj

3. fatal error LNK1120: 2 unresolved externals


Basically, this is one of those times when the compiler seems to output a whole bunch of bogus crap . Any Ideas?
Last edited by Wiki_Tiki; Jul 23rd, 2008 at 8:14 pm.
Reputation Points: 23
Solved Threads: 3
Junior Poster in Training
Wiki_Tiki is offline Offline
60 posts
since Jul 2008
Jul 23rd, 2008
1

Re: Visual C++ 2008 playing a sound

It isn't bogus crap. Learn to understand it and you'll begin to be able to solve problems on your own, though its understandable that at first you'll have no idea what it means.

The compilers complaining that the function PlaySound is declared somewhere, but it has no implementation. I'm actually not sure why it said it twice. The implementation would either be in the form of a .cpp file, or, in this case, a .lib file.

To fix this you need to link to the .lib winmm.lib. A simple way to do this is to add
C++ Syntax (Toggle Plain Text)
  1. #pragma comment(lib,"winmm.lib")
in one of your source files.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 24th, 2008
0

Re: Visual C++ 2008 playing a sound

Wow man, thanks a ton. (I know, my questions always sound retarded )
So simple to add a pragma comment...

Oh well, I'm a complete noob at VC++

But thanks a lot for the help, you were great
Last edited by Wiki_Tiki; Jul 24th, 2008 at 1:06 am.
Reputation Points: 23
Solved Threads: 3
Junior Poster in Training
Wiki_Tiki is offline Offline
60 posts
since Jul 2008
Jan 7th, 2009
0

Re: Visual C++ 2008 playing a sound

You could use the Beep command...

Beep(523,500); // 523 hertz (C5) for 500 milliseconds
Beep(587,500);
Beep(659,500);
Beep(698,500);
Beep(784,500);

but you probably didn't want to hear that from a novice like me...
this worked
thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hnsuper is offline Offline
1 posts
since Jan 2009
Dec 28th, 2009
-1
Re: Visual C++ 2008 playing a sound
I can not include both, "windows.h" and "Form2.h"

help !!!??? =(
Reputation Points: 9
Solved Threads: 0
Newbie Poster
toltec is offline Offline
1 posts
since Dec 2009
Oct 3rd, 2010
0
Re: Visual C++ 2008 playing a sound
Go to Project -> Properties -> C/C++ -> General and where it says something about Unicode, change it to Not Set. That solves all errors to do with Lpcwstr!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Quantumcat is offline Offline
1 posts
since Oct 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Display Array in listbox
Next Thread in C++ Forum Timeline: Help with stack





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC