Sign in to follow this  
EDEV

I've wasted 5 hours trying to figure out how to implement sound in my C++ program.

Recommended Posts

Hello, I've been using C++ these past few days, and I've been learning a lot and having fun. Right now I'm making small short games to test what I've learned so far. I want to play a song in one of my games...but I cannot figure out FOR THE LIFE OF ME, how to include/tie/associate libraries/headers so that the code will run. Right now I'm using Dev++, I've tried EVERYTHING, I've used devpaks, copied files into the include and lib folders...added libraries manually. Everytime I copy/paste some example code that should play a sound, I get linker errors and other errors out the wazoo. Is there a resource that will show me.

1. How to include files needed to get libraries to work/run on my compiler?
2. A VERY BASIC tutorial on how to get sound working with any library? The less calls/work I have to do the better. I just want something simple...playing a sound in my program. I figured out how to do this in VB in 10 minutes....5 hours in C++ has gotten me nowhere and its extremely discouraging.

Any help is VERY appreciated!

Share this post


Link to post
Share on other sites
Windows 7, file is .mp3/.wav, but I can transfer to any other format.

My first and biggest problem is Dev++ won't find/utilize any of the libraries, so when I use functions/code from examples, they don't compile they just give me errors.

Share this post


Link to post
Share on other sites
EDIT: Actually, I forgot to ask first, are you using some kind of external libraries? I don't mean source code, I mean things that maybe end in .a or .so or something like that.

I believe when you add external libraries to your project, you have to add them as arguments to your linker. Somewhere in your project settings you should have an option for additional arguments to pass to the linker where you'll have to add a line that resembles more or less: "-l<libraryName>" (obviously replacing <libraryName> with the actual name). What library are you using exactly?

DevC++ is an uber old IDE, unmaintained, and very buggy at that, so switching to something more modern will probably save you a whole lot of pain later on.
Learning to program with C++ can indeed be discouraging :D That's why people on the forums usually recommend something like C#, Python, or Java for people who are getting started.

Also,

Share this post


Link to post
Share on other sites
Your right about the IDE being old, its just the book I was using to follow along use this IDE. I'm going to us MS VC++, the code should just port over no problems, and I will see if I can get the libraries working from there.

Share this post


Link to post
Share on other sites
I have to strongly second boogymans' suggestion, DevC++ is probably making your life more difficult than it needs to be. If you are intending to stick with C++ I would download [url="http://www.microsoft.com/express/Downloads/#2010-Visual-CPP"]Visual C++ Express 2010[/url] which is completely free. Less bugs, better documentation and easier to get support from.

Now given its win32 and you are trying to play a WAV [url="http://msdn.microsoft.com/en-us/library/dd743680(v=vs.85).aspx"]the Win32 function PlaySound()[/url] is about as easy as it gets.

It looks complicated, but in reality boils down to:

[size="2"]PlaySound(TEXT("yourfile.wav"), NULL, SND_FILENAME);[/size]

[size="2"][font=Arial]Just be sure to include winmm.lib in your libraries.[/font]

[font="Arial"]First though, download VS2K10, it really will make your life a ton easier. [/font][/size]

Share this post


Link to post
Share on other sites
Wait so Visual C++ has a native call that I can use to play sound? No libraries involved? That's awesome!!!

2nd, whats VS2K10?

(In the midst of installing Visual C++ now)

Share this post


Link to post
Share on other sites
/* Scratch this part, Serapth probably knows better than I do */. It's from the win32 API which is probably what you're using now. As Serapth mentioned, you'll have to add winmm.lib to the list of libraries.

VS2K10 stands for Visual Studio 2010

Share this post


Link to post
Share on other sites
[quote name='EDEV' timestamp='1305391326' post='4810732']
Wait so Visual C++ has a native call that I can use to play sound? No libraries involved? That's awesome!!!

2nd, whats VS2K10?

(In the midst of installing Visual C++ now)
[/quote]

VS2K10 == VIsual Studio 2010 written by a lazy person. You are installing what I want you to.

As to Visual C++ having a native call, thats right but misleading. It's actually part of the WIN32 API. It's a small grammatically distinction but it is an important one. For example, you could have actually called PlaySound just as effectively from DevC++ assuming you were targeting windows. It has nothing at all to do with Visual C++. Make sense?

Share this post


Link to post
Share on other sites
Ok I installed it and got it running, I copied my code over, but I can't find where I go to run/test the code?

EDIT: Is it debug? The option to debug is greyed out so...I'm not sure...maybe theres something I gotta add to my code first?

Share this post


Link to post
Share on other sites
I think there should be a Build menu on the task bar at the top, I believe there is an option to build/run the code (I haven't use that IDE in soooo long XD)

BTW, you'll have to have your code belonging to a project before you can compile it (project settings constitute compiler/linker settings and such).

Share this post


Link to post
Share on other sites
Okay I got the project started, created a new .cpp and copied everything over.

I hit debug and I get an error:

Unable to start program 'c:\users\xxxx\documents\visualstudio2010\projects\dw1.1ported\release\dw1.1ported.exe'

system cannot find the specified file

ugh...what now?

Share this post


Link to post
Share on other sites
[quote name='EDEV' timestamp='1305392627' post='4810743']
Okay I got the project started, created a new .cpp and copied everything over.

I hit debug and I get an error:

Unable to start program 'c:\users\xxxx\documents\visualstudio2010\projects\dw1.1ported\release\dw1.1ported.exe'

system cannot find the specified file

ugh...what now?
[/quote]

EDIT: Working now, weird.

Let me test the game and see if everything is working...then I'll try to play the sound.

Share this post


Link to post
Share on other sites
PlaySound(TEXT("yourfile.wav"), NULL, SND_FILENAME);

Question here:

Am I supposed to put something where it says text? ANd where it says "yourfile.wav" do I just put the directory path where the file can be found?

EDIT: Also can you guys recommend a resouce for adding libraries? Not sure where I add in winmm.lib?

Share this post


Link to post
Share on other sites
[quote name='boogyman19946' timestamp='1305393130' post='4810748']
TEXT() is a macro so it should stay like it is :) yourfile.wav I imagine is the path to the wav file so you should replace it with the directory.
[/quote]

Alright, I tried following this guide:

http://sfml-dev.org/tutorials/1.6/start-vc.php

To add a library to the project, but when I go there it says its been deprecated and now its in a user sheet....

What the heck does that mean?!

Share this post


Link to post
Share on other sites
[quote name='EDEV' timestamp='1305389844' post='4810718']
1. I've wasted 5 hours trying to figure out how to implement sound in my C++ program.
2. I've been learning a lot and having fun.
[/quote]
1. It was NOT a waste.
2. See??

Share this post


Link to post
Share on other sites
[quote name='EDEV' timestamp='1305393214' post='4810751']
[quote name='boogyman19946' timestamp='1305393130' post='4810748']
TEXT() is a macro so it should stay like it is :) yourfile.wav I imagine is the path to the wav file so you should replace it with the directory.
[/quote]

Alright, I tried following this guide:

[url="http://sfml-dev.org/tutorials/1.6/start-vc.php"]http://sfml-dev.org/....6/start-vc.php[/url]

To add a library to the project, but when I go there it says its been deprecated and now its in a user sheet....

What the heck does that mean?!
[/quote]

[url="http://ctp.social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/137ab8bb-3296-40fa-a602-998bb6747829"]http://ctp.social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/137ab8bb-3296-40fa-a602-998bb6747829[/url] This should be more helpful.

I believe you can open the project properties by right-clicking on your project and selecting the properties option from the drop down menu (I don't know how 2010 is organized. I'm trying to install it atm)

Share this post


Link to post
Share on other sites
[quote name='boogyman19946' timestamp='1305393571' post='4810757']
[quote name='EDEV' timestamp='1305393214' post='4810751']
[quote name='boogyman19946' timestamp='1305393130' post='4810748']
TEXT() is a macro so it should stay like it is :) yourfile.wav I imagine is the path to the wav file so you should replace it with the directory.
[/quote]

Alright, I tried following this guide:

[url="http://sfml-dev.org/tutorials/1.6/start-vc.php"]http://sfml-dev.org/....6/start-vc.php[/url]

To add a library to the project, but when I go there it says its been deprecated and now its in a user sheet....

What the heck does that mean?!
[/quote]

[url="http://ctp.social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/137ab8bb-3296-40fa-a602-998bb6747829"]http://ctp.social.ms...02-998bb6747829[/url] This should be more helpful.

I believe you can open the project properties by right-clicking on your project and selecting the properties option from the drop down menu (I don't know how 2010 is organized. I'm trying to install it atm)
[/quote]

Okay followed that guide and added winmm.lib in the linker...

When I compile I get this:

1>c:\users\edgar\documents\visual studio 2010\projects\test\test\test2.cpp(75): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\edgar\documents\visual studio 2010\projects\test\test\test2.cpp(108): error C2065: 'SND_FILENAME' : undeclared identifier
1>c:\users\edgar\documents\visual studio 2010\projects\test\test\test2.cpp(108): error C3861: 'PlaySound': identifier not found
1>c:\users\edgar\documents\visual studio 2010\projects\test\test\test2.cpp(108): error C3861: 'TEXT': identifier not found
1>c:\users\edgar\documents\visual studio 2010\projects\test\test\test2.cpp(1065): warning C4390: ';' : empty controlled statement found; is this the intent?


Do I have to type something in the beginnign like #include <winmm>?

Share this post


Link to post
Share on other sites
[quote name='boogyman19946' timestamp='1305394139' post='4810760']
You should have #include <Windows.h> according to MSDN.
[/quote]

Alright well that's progress, the program started, but no sound...hmmm...

Share this post


Link to post
Share on other sites
I GOT IT WORKING!!!!

OMG THANK YOU ALL SO MUCH!!!

I appreciate all your patience and help with a complete C++ noob, you guys helped and responded much faster than I expected! I finally feel like I am understanding libraries.

Thanks for everything!

That being said...I have another problem...of course. XD

The song plays, but the program does not continue running, I want the song to run in the background while the game continues...right now it seems to stop...play the whole song, then move on.

Anyone know what I can do about that?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this