473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 2769
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian Basquille | last post by:
Hello all, What is the syntax for simply playing a WAV file in your program? Am learning VB and have two VB books but don't wanna root through them to find the code. Any help would be much...
1
by: Kiteman \(Canada\) | last post by:
Instead of a beep sound playing as an alarm or reminder I would like to be able to play a more interesting sound. On the web, I found and tried a sound routine that uses the SOUND.DRV but it does...
2
by: Dave | last post by:
Hi, I am writing a c# application that using a directshow to play file and display it on my C# gui, its work just fine but when i try to open another thread in my c# application the file stop...
7
by: Lee Moody | last post by:
I just want quick and easy way to play a .wav file out the standard sound device. It could even be as simple as activating a sound assigned to an existing windows sound event. Any suggestions?...
8
by: shlomi | last post by:
Hello, It's been long time that I'm investigating on how to play Avi file from memory, with no success. What I'm trying to do is to get a scrambled Avi file to memory, fix same data and...
1
by: Peter Schmitz | last post by:
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...
0
by: Raven Jones | last post by:
Heya all, I'm working on a web-based application (using ASP.NET and C# on .NET 1.1.4322, supporting only IE6 for Windows) that allows for file uploads. Screen real estate is at a premium, so I...
1
by: majestik | last post by:
Question, can I see an event in Javascript that tells me a video starts playing using the <embed src="file.wmv>. I stream the video, but there is a wait time and wanted to pop up a message telling...
5
by: gazza67 | last post by:
Hi, Does anyone know how to check for when a sound has finished playing? I am currently using the SoundPlayer, there doesnt seem to be any event for this - am I missing something? Gary
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.