associate sounds with buttons
-
I have a form layed out in Visual Studio .NET Professional, and I have 30 buttons layed out, I have sound files on the hard drive, and I would like to associate those sounds with those buttons. So when the Sounds.exe is run, and I press the first button then the sound is automatically played. Is there a way to link the sound to the button? And also when I build the .exe file is the sound file added to the .exe file? I am all new to this, and have created some code from a webpage that talked about embedding sounds, so I used the code as you see below : 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 hello1.wav Dim WavStrm As IO.Stream = _ Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _ NameSpc + "." + "hello1.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 The hello1.wav file is the file I wanted to play when i ran the app. If you want to see my source code you can get it here : h**p://home.pacific.net.au/~jf3000/sounds.zip Id appreciate any help at all on this, and just let me know how to get the first button going so I know where to take it from there. You will have to tell me in the most earliest of beginners terms, cos im still trying to understand something ive only just started. Someone mentioned in a forum to play a sound use this : PlaySound() but that lost me. samitha
-
I have a form layed out in Visual Studio .NET Professional, and I have 30 buttons layed out, I have sound files on the hard drive, and I would like to associate those sounds with those buttons. So when the Sounds.exe is run, and I press the first button then the sound is automatically played. Is there a way to link the sound to the button? And also when I build the .exe file is the sound file added to the .exe file? I am all new to this, and have created some code from a webpage that talked about embedding sounds, so I used the code as you see below : 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 hello1.wav Dim WavStrm As IO.Stream = _ Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _ NameSpc + "." + "hello1.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 The hello1.wav file is the file I wanted to play when i ran the app. If you want to see my source code you can get it here : h**p://home.pacific.net.au/~jf3000/sounds.zip Id appreciate any help at all on this, and just let me know how to get the first button going so I know where to take it from there. You will have to tell me in the most earliest of beginners terms, cos im still trying to understand something ive only just started. Someone mentioned in a forum to play a sound use this : PlaySound() but that lost me. samitha
If your going to have 30 sounds, don't embedded them as resources, it'll just make compiling your app that much longer, especially if they're large sound files. Just leave them as files. The code is really easy when you don't have all the junk of resources to look through. If your just starting out -> KEEP IT SIMPLE!
Private Declare Ansi Function PlaySound Lib "winmm.dll" Alias "sndPlaySoundA" \_ (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long Private Enum PlaySoundFlags Sync = &H0 Async = &H1 \[Loop\] = &H8 NoStop = &H10 NoDefault = &H2 End Enum Private Sub btnPlaySoundSync\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaySoundSync.Click Me.Cursor = Cursors.WaitCursor PlaySound("C:\\Program Files\\ahead\\Nero\\Beeth5th.wav", PlaySoundFlags.Sync) Me.Cursor = Cursors.Default End Sub
Private Sub btnPlaySoundAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaySoundAsync.Click
Me.Cursor = Cursors.WaitCursor
PlaySound("C:\Program Files\ahead\Nero\Beeth5th.wav", PlaySoundFlags.Async)
Me.Cursor = Cursors.Default
End SubThis code assumes you have two buttons on your form, named
btnPlaySoundSync
andbtnPlaySoundAsync
. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome