Categories
Uncategorized

Play, Pause and Stop an audio in HTML 5 by example

HTML 5’s audio tag has been around for a while now but, I never got a chance to try them. Today finally I got the chance, so here is what I created using jsfiddle.

Live Demo

HTML

<input type="button" value="play" id="playBtn" />
<input type="button" value="pause" id="pauseBtn" />
<input type="button" value="stop" id="stopBtn" />

Jquery

var source = "http://www.giorgiosardo.com/HTML5/audio/audio/sample.mp3"
var audio = document.createElement("audio"); audio.src = source;
$("#playBtn").click(function () {
audio.play();
});
$("#pauseBtn").click(function () {
audio.pause();
});
$("#stopBtn").click(function () {
audio.pause();
audio.currentTime = 0;
});
Hope this helps :)