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.
[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]
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.