INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."
Geography
Where in the world do Tek-Tips members come from?
|
Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ
|
Embedded Resources
|
Embedding sounds into your application.
Posted: 22 Jan 04
|
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 Imports System statement at the top of your code file.
This example is for a sound that is used for a button click.
Class SoundButton Inherits Button
'API call for playing sounds in memory Private Declare Function PlaySound Lib "winmm.dll" (ByVal data() As Byte, _ ByVal hMod As IntPtr, ByVal hwFlags As Integer) As Integer Private Const SND_ASYNC As Integer = &H1 'Play asynchronously Private Const SND_MEMORY As Integer = &H4 'Play wav in memory
'The .wav will be stored in this byte array Private Shared ClickSound As Byte() Shared Sub New() 'Get running assembly name Dim NameSpc As String = _ Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() 'Look for the button click sound in the resource stream. 'This example has a resource called ButtonClick.wav Dim WavStrm As IO.Stream = _ Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _ NameSpc + "." + "ButtonClick.wav") 'ReDim the byte array to be the size of the embedded .wav ReDim ClickSound(CType(WavStrm.Length, Integer)) 'Load the .wav from the stream into the byte array WavStrm.Read(ClickSound, 0, Int(CType(WavStrm.Length, Integer))) End Sub
'Override the OnClick event to play the sound Protected Overrides Sub OnClick(ByVal ea As EventArgs) Call PlayWav(ClickSound) MyBase.OnClick(ea) End Sub
'Play embedded .wav resource Public Sub PlayWav(ByVal WavResource As Byte()) PlaySound(WavResource, IntPtr.Zero, SND_ASYNC Or SND_MEMORY) End Sub End Class
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.
|
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ Index
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 Forum
My FAQ Archive
Email This FAQ To A Friend |
|
 |
|