INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • 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.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Embedded Resources

Embedding sounds into your application. by Sypher2
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 FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close