Drydo's Blog

Teenager of the Internet

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login   Community Forums   :: vbCity.com   :: DevCity.NET  

Just a quickie.

I keep seeing questions on the forum (www.vbcity.com/forum) about WAVs, Resource Files and all that malarkey and I'd thought I'll blog up the code for all sundry. Let it not be said that I am not a fan of Resource Files oh observers.

Anyhoo, the codey will extract a WAV file at runtime and play the sound by passing the byte data to the sndPlaySound API function. Note: to include a WAV into your assembly, simply add the file (as an existing file) to your project and set its 'Build' option to 'Embedded Resource'.

Code Copy
' Declarations...

' Play Sound flags

Private
Const SND_ASYNC As Integer = &H1
Private
Const SND_LOOP As Integer = &H8
Private
Const SND_MEMORY As Integer = &H4
Private
Const SND_NODEFAULT As Integer = &H2
Private
Const SND_NOSTOP As Integer = &H10
Private
Const SND_SYNC As Integer = &H0

' Play Sound declaration

Declare
Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As Byte(), ByVal uFlags As Integer) As Integer

' Calling Code...


' Name of the resource to extract

Dim
rName As String = "cameraclick.wav"
' Obtain the assembly name and replace '-' with an underscore

Dim
rSchema As String = Replace(Reflection.Assembly.GetExecutingAssembly.GetName.Name, "-", "_") & "." & rName
' Retreive the resource as a stream

Dim
rStream As IO.Stream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(rSchema)
' Read the stream into a byte buffer

Dim
buffer(rStream.Length) As Byte
rStream.Read(buffer, 0, buffer.Length - 1)
' Play the sound

sndPlaySound(buffer, SND_MEMORY Or SND_ASYNC)

M

posted on Thursday, August 11, 2005 3:46 PM