471,325 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Playing *.wav file

Hi,

I'm just trying to play a *.wav file, making use of the unmanaged function
"sndPlaySoundA" in winmm.dll, but I still got one question: How can I avoid,
that the sound file itself has to be seperate file in a specific directory,
but instead 'integrate' it in my application?

Thanks
peter
Jul 21 '05 #1
1 2620
Hi Peter,

I created a game programming library for .Net in VB.Net. Here is how I used
the Windows API to play sound before I switched to FMod from www.fmod.org .
Below are the support functions:

#Region " Sound Support"
Public Enum SND_FLAGS
SND_SYNC = &H0
SND_ASYNC = &H1
SND_NODEFAULT = &H2
SND_MEMORY = &H4
SND_LOOP = &H8
SND_NOSTOP = &H10
SND_PURGE = &H40
SND_ALIAS = &H10000
SND_RESOURCE = &H4004
SND_NOWAIT = &H2000
End Enum
Private Declare Function PlayASound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As
Integer) As Integer

Private Declare Function PlayASound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal arrSound() As Byte, ByVal hModule As Integer, ByVal dwFlags As
Integer) As Integer

Public Sub kfd_PurgeSound()
PlayASound("", 0, SND_FLAGS.SND_PURGE)
End Sub

'Plays a .Wav file.

'---------------------------------------------------------------------------
-----------------
'If QuickReturn is TRUE the function returns without waiting for the
sound to finish playing.
'If LoopIt is TRUE the function plays the sound over and over again till
StopSound() is called.
'If MustPlay is TRUE and another sound is currently playing the function
returns 0 telling you
' that the sound you just sent did not play. If this value is False
then the sound you send
' will end the currently playing sound and then play.
Public Function kfd_PlaySound(ByVal arrSound() As Byte, Optional ByVal
QuickReturn As Boolean = True, Optional ByVal LoopIt As Boolean = False,
Optional ByVal MustPlay As Boolean = False) As Integer
Dim intFlags, intResult As Integer
intFlags = SND_FLAGS.SND_NODEFAULT + SND_FLAGS.SND_MEMORY
If (QuickReturn = True) Then
intFlags += SND_FLAGS.SND_ASYNC
Else
intFlags += SND_FLAGS.SND_SYNC
End If
If (LoopIt = True) Then
intFlags += SND_FLAGS.SND_LOOP
End If
If (MustPlay = True) Then
intFlags += SND_FLAGS.SND_NOSTOP
End If
'Returns 1 if it worked, 0 if it didn't.
intResult = PlayASound(arrSound, 0, intFlags)
End Function

'Stops the .Wav file that is currently playing.
Public Function kfd_StopSound() As Integer
PlayASound(vbNullString, 0, (SND_FLAGS.SND_PURGE +
SND_FLAGS.SND_NODEFAULT))
End Function

Public Function kfd_FillSoundArray(ByVal mstrSound As String) As Byte()
Dim fn As Long
Dim b() As Byte
fn = FreeFile()
FileOpen(fn, mstrSound, OpenMode.Binary)
ReDim b((LOF(fn) - 1))
FileGet(fn, b)
FileClose(fn)
Return b
End Function
#End Region

Below is the code that plays a sound when a player gets hit using the code
above.

Dim sndPlayerHit() As Byte 'A byte array to hold the player getting hit
sound.
sndPlayerHit = kfd_FillSoundArray(strAppPath + "Sounds\PlayerHit.Wav")
kfd_PlaySound(sndPlayerHit)

The first line is in my declarations section. The second line is run in my
page load to preload the sound file into an array. The 3rd line is run
everytime the player is hit with a bullet. Note that the PlayASound API is
overloaded so you can call that directly with a string representing the path
if you don't preload your sound into an array, or rewrite your own version
of kfd_PlaySound to accept a file path. I would recommend moving to FMod
because it let's you play all your sounds and merges the audio, where as
with the Windows API you can only play one sound at a time and if you want
to play another sound the one currently playing gets interrupted. Good
luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Peter Schmitz" <Pe**********@discussions.microsoft.com> wrote in message
news:4A**********************************@microsof t.com...
Hi,

I'm just trying to play a *.wav file, making use of the unmanaged function
"sndPlaySoundA" in winmm.dll, but I still got one question: How can I avoid, that the sound file itself has to be seperate file in a specific directory, but instead 'integrate' it in my application?

Thanks
peter

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brian Basquille | last post: by
1 post views Thread by Kiteman \(Canada\) | last post: by
2 posts views Thread by Dave | last post: by
7 posts views Thread by Lee Moody | last post: by
8 posts views Thread by shlomi | last post: by
1 post views Thread by Peter Schmitz | last post: by
5 posts views Thread by gazza67 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.