Visual Basic .Net - Button Sound / Music Play
Visual Basic 2010 - Button Sound
Visual Basic 2010 - Play sound on buttons |
The code is simply goes like this
You can copy and paste in your (Click_Event) or whatever it is you wish (MouseClick - MouseHover - MouseLeave) :
- Dim MySound As New System.Media.SoundPlayer()
- MySound.SoundLocation = "path to the .Wav file"
- MySound.Load()
- MySound.Play()
' OR you can use this :
- Sub PlaySystemSound()
- My.Computer.Audio.PlaySystemSound(
System.Media.SystemSounds.Asterisk) - End Sub
Background playing
It lets the application execute other code while the sound plays. The My.Computer.Audio.Play method allows the application to play only one background sound at a time; when the application plays a new background sound, it stops playing the previous background sound. You can also play a sound and wait for it to complete.
For Visual Basic 2010 and in general, when an application plays a looping sound, it should eventually stop the sound. For more information, see How to: Stop Playing Sounds in the Background in Visual Basic 2010.
Example
The My.Computer.Audio.Play method plays a sound. When AudioPlayMode.WaitToComplete is specified, My.Computer.Audio.Play waits until the sound completes before calling code continues.
Check that the file name references a .wav sound file on your system.
- Sub PlayBackgroundSoundFile()
My.Computer.Audio.Play("C:\Waterfall.wav",
AudioPlayMode.WaitToComplete)
End Sub
The My.Computer.Audio.Play method plays a sound. When AudioPlayMode.WaitToComplete is specified, My.Computer.Audio.Play waits until the sound completes before calling code continues.
Check that the application resources include a .wav sound file named Waterfall.
- Sub PlayBackgroundSoundResource()
My.Computer.Audio.Play(My.Resources.Waterfall,
AudioPlayMode.WaitToComplete)
End Sub
♥ Here are some online Visual Basic lessons and courses :
- Visual Basic .Net snippets collection
- Visual Basic .Net - How to check for the internet connection
- Visual Basic .Net - POP3 and Receiving E-mails
- Visual Basic .Net - Generate Random Combinations
- Visual Basic .Net - Play sounds on Button Click or Mouse Hover
- Visual Basic .Net - Progressbar control
- Visual Basic .Net Solution - The application failed to initialize
- Visual Basic .Net - Working with Database full example
- Visual Basic .Net - There is already an open DataReader associated
- Visual Basic .Net - SyBase Advantage Database [Add, Edit, Search, Delete and DataGridView]
- Visual Basic .Net - Math Functions
- Visual Basic .Net - Communicating with clients [Requirements Docs, Use-Case]
- Visual Basic .Net - Compare Two Tree-Views and return results
- Visual Basic .Net - Free POS System Project Source Code