User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,097 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,317 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 509 | Replies: 6 | Solved
Reply
Join Date: Jul 2008
Posts: 60
Reputation: Wiki_Tiki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Wiki_Tiki Wiki_Tiki is offline Offline
Junior Poster in Training

Visual C++ 2008 playing a sound

  #1  
Jul 23rd, 2008
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:

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 5:38 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: Visual C++ 2008 playing a sound

  #2  
Jul 23rd, 2008
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:
PlaySound((LPCWSTR)"beep.wav", 0, SND_LOOP|SND_ASYNC);
will work.
Last edited by CoolGamer48 : Jul 23rd, 2008 at 5:44 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Join Date: May 2008
Location: Vineland, NJ
Posts: 29
Reputation: Pikachumanson is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Pikachumanson Pikachumanson is offline Offline
Light Poster

Re: Visual C++ 2008 playing a sound

  #3  
Jul 23rd, 2008
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...
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Visual C++ 2008 playing a sound

  #4  
Jul 23rd, 2008
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 6:25 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 60
Reputation: Wiki_Tiki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Wiki_Tiki Wiki_Tiki is offline Offline
Junior Poster in Training

Re: Visual C++ 2008 playing a sound

  #5  
Jul 23rd, 2008
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 7:14 pm.
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: Visual C++ 2008 playing a sound

  #6  
Jul 23rd, 2008
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
#pragma comment(lib,"winmm.lib")
in one of your source files.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Join Date: Jul 2008
Posts: 60
Reputation: Wiki_Tiki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Wiki_Tiki Wiki_Tiki is offline Offline
Junior Poster in Training

Re: Visual C++ 2008 playing a sound

  #7  
Jul 23rd, 2008
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 12:06 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC