Back to Forums








View Full Version : Question in C++ programming (Using .WAV files)


Linedawg
May 9th, 2007, 13:53 PM
Hello guys, I have a stupid question. I was programming a GUI menu screen after I got every thing working I decided to place a wave file to run in the back ground to add more depth to my application. But I realized I have no clue on how to trigger a sound file. Dose any one knows how to write a function, class or even a header file to allow for sounds? Also can I use C++ to active and search for the default soundcard on any windows system or do I have to use direct X?

I try to add this to my main with in the code:

Public Class SoundClass
Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer
' name specifies the sound file when the SND_FILENAME flag is set.
' hmod specifies an executable file handle.
' hmod must be Nothing if the SND_RESOURCE flag is not set.
' flags specifies which flags are set.

' The PlaySound documentation lists all valid flags.
Public Const SND_MEMORY = &H4 'play stream
Public Const SND_NOSTOP = &H10 ' no stop
Public Const SND_SYNC = &H0 ' play synchronously
Public Const SND_ASYNC = &H1 ' play asynchronously
Public Const SND_PURGE = &H100 ' purge non-static event for task
Public Const SND_LOOP = &H8 ' loop
Public Const SND_NODEFAULT = &H2 ' silence if no file
Public Const SND_FILENAME = &H20000 ' name is file name
Public Const SND_RESOURCE = &H40004 ' name is resource name or atom

Public Sub PlaySoundStream(ByVal filename As String)
' Plays a sound from filename.
PlaySound(filename, Nothing, SND_MEMORY Or SND_ASYNC Or SND_LOOP)
End Sub

Public Sub PlaySoundFile(ByVal filename As String)
' Plays a sound from filename.
PlaySound(filename, Nothing, SND_FILENAME Or SND_ASYNC Or SND_LOOP)
End Sub
Public Sub StopSound()
' Plays a sound from filename.
PlaySound(Nothing, Nothing, SND_NODEFAULT Or SND_FILENAME)
End Sub

End Class


But I had no luck with it, I feel as if I’m missing some thing. So dose any one out there knows of a simple way to trigger a .WAV file to run in the back ground of my menu screen? Also dose any one know how to trigger a sound when you move a mouse curser over a button?

This has been driving my crazy for the past few days. I cant believe I have never tried this before lol but I’m stuck.

cash_site
May 10th, 2007, 03:44 AM
I remember trying to make some code in MFC c++.

The initial class setup was something similar to what you posted...

I've found some other links on MSDN library

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/htm/playsoundsample.asp

They have some samples at bottom too...

Also, another thread with similar question.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1502802&SiteID=1

This is the search from MSDN results
http://search.msdn.microsoft.com/search/Default.aspx?brand=msdn&query=play+wav+c%2b%2b

Linedawg
May 15th, 2007, 16:01 PM
Thanks Cash_site,
The links you posted where exactly what I was looking for. I can’t believe I have over looked it when searching MSND. Thanks for the awesome links.

Linedawg
May 15th, 2007, 18:38 PM
Also to address my Direct X question I found this link that has a pretty good tutorial on how to get started.
http://www.c-unit.com/tutorials/directx/?t=23

Just wanted to add this link into the tread so if any one else with the question can have a tutorial.

cash_site
June 26th, 2007, 02:34 AM
Good to hear LineDawg! You are getting pretty good at this software stuff, you'll have to post some links to your programs :D