473,799 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Playing *.wav file

Hi,

I'm just trying to play a *.wav file, making use of the unmanaged function
"sndPlaySou ndA" 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 2870
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_P URGE)
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(B yVal 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_N ODEFAULT + SND_FLAGS.SND_M EMORY
If (QuickReturn = True) Then
intFlags += SND_FLAGS.SND_A SYNC
Else
intFlags += SND_FLAGS.SND_S YNC
End If
If (LoopIt = True) Then
intFlags += SND_FLAGS.SND_L OOP
End If
If (MustPlay = True) Then
intFlags += SND_FLAGS.SND_N OSTOP
End If
'Returns 1 if it worked, 0 if it didn't.
intResult = PlayASound(arrS ound, 0, intFlags)
End Function

'Stops the .Wav file that is currently playing.
Public Function kfd_StopSound() As Integer
PlayASound(vbNu llString, 0, (SND_FLAGS.SND_ PURGE +
SND_FLAGS.SND_N ODEFAULT))
End Function

Public Function kfd_FillSoundAr ray(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_FillSoundAr ray(strAppPath + "Sounds\PlayerH it.Wav")
kfd_PlaySound(s ndPlayerHit)

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**********@d iscussions.micr osoft.com> wrote in message
news:4A******** *************** ***********@mic rosoft.com...
Hi,

I'm just trying to play a *.wav file, making use of the unmanaged function
"sndPlaySou ndA" 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
7142
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 appreciated! Thanks in advance,
1
9344
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 not work for me. Maybe it would be best to try to play a small mp3 or wav file instead - does VB 6.0 have built-in support for sounds like that? Tom
2
4479
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 playing and i get black image!!! Why? What i should considre when i open a new thread in c# application?? Thanks
7
6462
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? Thanks in advance. -Lee
8
2538
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 then playing it as Avi file. An alternative way is encapsulate the binary data into the exe file with a recourse file and than playing it.
1
448
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 seperate file in a specific directory, but instead 'integrate' it in my application? Thanks peter
0
2481
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 want to set its style to "display: none;" and interact only with the standard file upload dialog (which can be summoned by firing the click() method of the control). Attempting programmatic access of a file upload control is generally playing...
1
3313
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 the user it is buffering until the video actually begins to play in the browser. Thanks for any help! JJ
5
11137
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
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.