Question

wav/mp3 files in c++

Asked by: dgal

I have metrowerks codewarrior learning addition for the AP Comp Sci classes.  I am trying to add sound to my project.  I have figured a few things out just by looking on the internet but I am still having some difficulty.  Here is what I have so far:

#include <windows.h>
#include <mmsystem.h>
//I also have the winmm.lib included in the project

int main()
{
   //blah...do some stuff unrelated to sound
PlaySound("C:\Games\Sounds\Bang.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);

}

Now, the reason that I have this command is because I was looking at a bunch of old questions on this site.  I looked through the mmsystem header and tried to make some sense of it but I had difficulty.  I am not sure why the NULL needs to be here or what it does.  When I do compile the program I hear a sound but it is simply the windows ding sound or char(7).  Why am I not hearing the wav file?  Also, please explain how this function works, ie what the params do etc.  Thanks

dgal

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2001-02-12 at 14:45:17ID20076664
Tags

playsound

Topic

C++ Programming Language

Participating Experts
5
Points
50
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. playing .wav files in C
    ok....im new here....and new to C....im taking my second semester of C and was wondering if anyone knew how to use C to play .wav files....and if C has to do anything to the soundcard before it can play .wav files???
  2. windows.h and afxv_w32.h
    Hi, I am trying to combine a DLL in C with a stand alone codes that use MFC ( as a Shared DLL). The DLL codes in C make use of the windows.h file in the rc extension files. This conflict with the file afxv_w32.h in the directory "DevStudio\VC\MFC\include\". the l...
  3. use of getch() function in codewarrior
    I am writing a simple maze program, and am trying to use the getch() funtion in conio.h for inputing a move without pressing enter. I got getch() to work , but that was using visual c++, and when i brought it to school and used codewarrior to compile it didnt work. i have ...
  4. windows.h
    i have just installed redhat 6.0 on one of my pc's i havnt used redhat before but am reading up on the basics. i have successfully compiled a few .c programs using gcc and managed to get winsock.h HOWEVER I cannot find or obtain windows.h file... 1) where can i get windo...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: dgalPosted on 2001-02-12 at 14:54:03ID: 5836484

I forgot to mention that I am curious as to how I would play other media types such as mp3.  Thanks.

 

by: snedelchevPosted on 2001-02-12 at 17:42:02ID: 5836865

Today a have the same problem too
but if i compile program in directory where is my wave file
and playsound is like this a hear the sound
PlaySound("Bang.wav",NULL,SND_FILENAME|SND_ASYNC);

i think you must test what PlaySound retunrs
Fasle or True

 

by: zechisPosted on 2001-02-12 at 18:12:30ID: 5836923

The NULL indicates that you are loading the WAV file from an actual file on disk as opposed to a resource.  The alternate method is to add the WAV to your app's resource file, give it an ID, say IDR_WAVE_BANG or whatever, and call the function like this:

PlaySound( IDR_WAVE_BANG, GetModuleHandle( NULL ), SND_RESOURCE | SND_ASYNC );

Something like that.  

Drawback to using the resource version is that the whole app must be rebuilt if you change the sound and that your executable size goes up (since it gets added to your exe).

As to why you get the windows ding instead: because the system couldn't find the WAV file that you specified in the directory you sepcified OR in the search path.  Make sure the file exists where you say (also, make sure that the string is correct: ie above should be "c:\\games\\sounds\\bang.wav"

For playing mp3, you need to use a third-party library (can't think of any free ones, try a web search), or roll your own decode routines (not a trivial task!!!).

Hope this helps!

 

by: conchitaPosted on 2001-02-12 at 18:38:18ID: 5836966

The problem is that you got the file name wrong, as zechis said. You need to use '\\' for each backslash.

For documentation on hte function: http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/multimed/mmfunc_9uxw.htm

 

by: dgalPosted on 2001-02-13 at 10:42:42ID: 5839601

Thanks zechis.  I am wondering how I can play a wav in the back and have other wavs play over that.  Can you explain how to do that?  Thanks.

dgal

 

by: zechisPosted on 2001-02-13 at 14:35:22ID: 5840299

The SND_ASYNC flag that you passed in the third parameter tells PlaySound() to return right after starting the sound and not to wait for it to finish.  So to play another song at the 'same time', just call PlaySound() again.  

If you were to specify SND_SYNC, PlaySound() would not return until the wav finished playing.  That's probably why they don't let you use SND_LOOP with SND_SYNC: because you could hang the process because Playsound would never return!

I used 'same time' in quotes above because you really aren't playing the sound at exactly the same time.  There is some overhead involved (loading the file, etc.) which could slow down the start of the second, so they would be a bit out of step.  [of course, you could use this to your advantage: by playing the same sound, you would get an echoing effect!]

If you truly want to play sounds with that much precision though, use DirectSound.

 

by: dgalPosted on 2001-02-13 at 15:42:12ID: 5840420

I tried doing that but it does not work.  Here is the code that I have:

PlaySound("C:\\Games\\Sounds\\Bang.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
     PlaySound("C:\\Games\\Sounds\\Effect.wav",NULL, SND_FILENAME|SND_ASYNC);

I want bang.wav in a loop in the back and I want effect.wav to be played once in front of bang but still have bang play.  Instead, I only hear one instance of effect.wav.  However, I did compile it several times trying several things and once when I had this exact code, I heard bang for a split second then effect.wav took over. I think that bang.wav is getting cut off.  Any ideas?  Thanks.

dgal

 

by: dgalPosted on 2001-02-13 at 15:42:40ID: 5840421

Also, what is DirectSound?

 

by: zechisPosted on 2001-02-13 at 16:12:35ID: 5840483

Oops, I forgot that PlaySound can't mix waveforms.  I think it uses a single buffer, so when you load a new sound, it replaces the first.  I have been using DirectSound (now DirectAudio) for a while now and forgot all of the standard API function peculiarities.  Hmm, try the MCI functions if you can't use DS/DA, but DirectAudio is the way to go.

As for DirectSound: it is part of the DirectX API that gives apps direct access to the hardware while still keeping it at a high enough level that the app doesn't care which hardware is attached.  DirectSound is the part of the API that deals with sound.  There is also a DirectGraphics, DirectInput, DirectPlay, etc.

Perhaps it can be explained better here: www.microsoft.com/directx

If you look at this site and still have questions, let me know.  It actually is a lot easier than it looks! The hardest part is downloading the ~120MB SDK you need to develop DirectX apps :)  

 

by: dgalPosted on 2001-02-13 at 16:55:23ID: 5840569

If I end up using directx's sound stuff, could I still use my regular ide etc and just include some file?  Is there any chance that you could point me straight to the sdk?  As for the download, that wont be a problem since my school is connected on dual t1.  Is this sdk free?  If not, do you know of any headers etc that I can use that are free?  Thanks/

 

by: dgalPosted on 2001-02-13 at 17:18:31ID: 5840610

Ok...I am downloading the sdk so I don't need your help wiht that one.  As far using it though, could you explain how to?  I mean, could I still use my ide?  Do I need to do new things?  What functions do I use?  How do I impliment them?  Thanks.

 

by: zechisPosted on 2001-02-14 at 11:45:42ID: 5843356

You should be able to continue using your IDE, unless it has problems linking ms format libraries or reading ms header files (this is quite possible actually, the win32 sdk that came with your compiler may have been tailored for it).  

No new things really, just like you added winmm.lib you have to add the directx libraries, and just like you included mmsystem.h you must include a couple of the directx headers.  

As to the functions and the implementation, check out the sample apps in the sdk, there's quite a few but the help file is pretty good.  Start with the small ones and the tutorials.  I know it doesn't make much sense but the smallest/easiest tutorial for playing a wav file is in the DirectMusic directory, tutorial #1.

One final note: this may be a bit overkill for what you need to do.  The directx API is very powerful (it is used to make most [all?] of the PC video games and mutlimedia programs out there, even DVD players!), and flexible.  But with this comes the need to learn a lot of new terms/lingo/etc.  If you have a deadline, you may want to skip this and go for the simpler approach, i.e. don't use simultaneous sound effects.  Also, if it is for a school project, it is possible the school will not be totally up to date when it comes to running dx8.  Dx does require windows 98 or ME, or windows 2000.

But, if you've got the will, the interest, and the time, I can still help.  I am better at the graphics portion of directx but I have some old sound code laying around that may be useful!

 

by: dgalPosted on 2001-02-19 at 15:24:48ID: 5857545

I took a look at that folder but there is only a cpp file and two other files of type dsp and dsw.  I have no program that can open these types of files.  The cpp is not descriptive at all.  Could you please share the syntax with me?  Thanks a lot.

dgal

 

by: griesshPosted on 2002-01-29 at 12:52:33ID: 6764756

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 "zechis" comment(s) as an answer.

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

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

 

by: MindphaserPosted on 2002-05-08 at 09:56:42ID: 6996919

Force accepted

** Mindphaser - Community Support Moderator **

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...