Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. associate sounds with buttons

associate sounds with buttons

Scheduled Pinned Locked Moved Visual Basic
csharptutorialvisual-studiohardwaredata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samithas
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • S samithas

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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 Sub

      This code assumes you have two buttons on your form, named btnPlaySoundSync and btnPlaySoundAsync. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups