473,394 Members | 1,693 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,394 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 2779
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.