HTML5 audio control stop button (as opposed to pause)

拟墨画扇 提交于 2019-12-05 11:20:56

If your play/pause is working, this should work for stop:

function stop() {
    var audioPlayer = document.getElementsByTagName('audio')[0];
    audioPlayer.pause();
    audioPlayer.currentTime = 0;
}

Here is a small demo code, where you can play, pause and stop an audio.

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;
});

Source

If you want to stop the music and abort the downloading operation you can use this code:

<button id="btnStop" onclick="player.src = ''; player.src = 'song.mp3';">Stop</button>

We are using java here to change the song src and then put it back there! Tricky but worky

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!