Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedding sounds into your application.

Embedded Resources

Embedding sounds into your application.

by  Sypher2  Posted    (Edited  )
You have some .wav sounds that you want to use with your application. However, you don't want to mess with the hassle of putting the .wav files on all the computers that use your app.

So what can you do?

Embed the sound as a resource.

.NET allows you to embed pretty much any file into the executable. It's just a matter of extracting the resource when you need it.

With sounds, there is an overload to the PlaySound API that takes a byte array.

Step 1: The first thing you need to do is add the sound to your solution. Go to the Project menu and select Add Existing Item (Ctrl-D shortcut). Navigate to the sound you want to use and click Open. The sound will be copied to your solution's folder, and you will see it in the Solution Explorer.

Step 2: Click on the sound in Solution Explorer to highlight it. In Properties (under Solution Explorer) there is a line for Build Action. This defaults to Compile. You need to change it to Embedded Resource by selecting from the drop down list.

(Repeat steps 1 and 2 for any additional sounds you want to embed.)

Step 3: Ok, now that the sound is an embedded resource, you just have to write the code to play it.

The following code assumes you have an [tt][color blue]Imports[/color] System[/tt] statement at the top of your code file.

This example is for a sound that is used for a button click.

[tt][color blue]Class[/color] SoundButton
[color blue]Inherits[/color] Button

[color green]'API call for playing sounds in memory[/color]
[color blue]Private Declare Function[/color] PlaySound [color blue]Lib[/color] "winmm.dll" ([color blue]ByVal[/color] data() [color blue]As Byte[/color], _
[color blue]ByVal[/color] hMod [color blue]As[/color] IntPtr, [color blue]ByVal[/color] hwFlags [color blue]As Integer[/color]) [color blue]As Integer[/color]
[color blue]Private Const[/color] SND_ASYNC [color blue]As Integer[/color] = &H1 [color green]'Play asynchronously[/color]
[color blue]Private Const[/color] SND_MEMORY [color blue]As Integer[/color] = &H4 [color green]'Play wav in memory[/color]

[color green]'The .wav will be stored in this byte array[/color]
[color blue]Private Shared[/color] ClickSound [color blue]As Byte[/color]()

[color blue]Shared Sub New[/color]()
[color green]'Get running assembly name[/color]
[color blue]Dim[/color] NameSpc [color blue]As String[/color] = _
Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
[color green]'Look for the button click sound in the resource stream.
'This example has a resource called ButtonClick.wav[/color]
[color blue]Dim[/color] WavStrm [color blue]As[/color] IO.Stream = _
Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _
NameSpc + "." + "ButtonClick.wav")
[color green]'ReDim the byte array to be the size of the embedded .wav[/color]
[color blue]ReDim[/color] ClickSound([color blue]CType[/color](WavStrm.Length, [color blue]Integer[/color]))
[color green]'Load the .wav from the stream into the byte array[/color]
WavStrm.Read(ClickSound, 0, Int([color blue]CType[/color](WavStrm.Length, [color blue]Integer[/color])))
[color blue]End Sub[/color]

[color green]'Override the OnClick event to play the sound[/color]
[color blue]Protected Overrides Sub[/color] OnClick([color blue]ByVal[/color] ea [color blue]As[/color] EventArgs)
[color blue]Call[/color] PlayWav(ClickSound)
[color blue]MyBase[/color].OnClick(ea)
[color blue]End Sub[/color]

[color green]'Play embedded .wav resource[/color]
[color blue]Public Sub[/color] PlayWav([color blue]ByVal[/color] WavResource [color blue]As Byte[/color]())
PlaySound(WavResource, IntPtr.Zero, SND_ASYNC [color blue]Or[/color] SND_MEMORY)
[color blue]End Sub[/color]
[color blue]End Class[/color][/tt]

This is just one example of how to use an embedded .wav sound. Normally you would read the .wav into a byte array variable when your application is loaded (or when the first instance of a control is created as in the above example). Anytime you want to play the .wav, simply pass the byte array to the PlayWav method.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top