Does anyone know the code for adding sound to a Button_Click Event?
I have searched Google but thought someone might be able to help here.
Thanks in advance :)
Printable View
Does anyone know the code for adding sound to a Button_Click Event?
I have searched Google but thought someone might be able to help here.
Thanks in advance :)
Listen for event.
Play sound with System.Media.SoundPlayer when event triggers.
????
PROFIT!
Off topic:
Do you think it's possible to add sound onclickbutton with HTML/Java Script?
Yes, but it's complicated. It's not as easy to play a sound using JavaScript and HTML when u click a button.
JavaScript: Playing Sound
You can play audio in a browser simply by putting a link to an audio file. With JavaScript you can use AJAX to load the audio file, and it'll start to play.
What you really want to do is preload the audio when the page loads. (normally audio takes a few seconds to download). If you click a button and use ajax to load the sound, there will be a significant delay before it plays.
If you really want this to function correctly- using preloading and events (such as onclick), you want to target HTML5's Media API.
4.8.6 The video element — HTML5
Example:
http://www.position-absolute.com/art...-manipulation/
Thanks!
I just want to add a button onclick sound.
Double click the button in visual studio and type in the soundplayer code...
The last link I gave you has something pretty much like this example (jQuery): [I modified it to remove the progress bar]
In regular JavaScript you can do the same with this:Code:$(document).ready( function()
{
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'audio_file.ogg');
audioElement.load()
//...
$('.play').click(function()
{
audioElement.play();
});
//...
}
I posted a very dumbed-down interactive example here:Code:onload = function()
{
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'audio_file.ogg');
audioElement.load()
//...
document.getElementById('play').onclick = function()
{
audioElement.play();
}
//...
}
JS Bin - Collaborative JavaScript Debugging
Note again, it only works in HTML5 browsers which support OGG audio.
Edit: Some people wonder why there's no 'stop' function in the spec. Stopping is turning off the audio (or pausing), and resetting the progress.
With that said, I implemented stop and a progress report:
http://jsbin.com/ededih/4/edit