Html HTML5 音频控制停止按钮(与暂停相反)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10653823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:30:14  来源:igfitidea点击:

HTML5 audio control stop button (as opposed to pause)

htmlcontrollerhtml5-audio

提问by yolise

I've been absolutely everywhere (I think) and can't seem to find a way to call a stop event in an html5 audio controller. My audio controller has a playlist where each track will play when selected or cycle through each track to the next. There's also a next button which works.

我绝对无处不在(我认为)并且似乎找不到在 html5 音频控制器中调用停止事件的方法。我的音频控制器有一个播放列表,其中每个曲目将在选择时播放或循环播放每个曲目到下一个。还有一个下一步按钮可以工作。

My play/pause button looks like this:

我的播放/暂停按钮如下所示:

`    function playPause() {
        var audioPlayer = document.getElementsByTagName('audio')[0];
        if(audioPlayer!=undefined) {
            if (audioPlayer.paused) {
                audioPlayer.play();
            } else {
                audioPlayer.pause();
            }
        } else {
            loadPlayer();
        }
    }
`

I need a "Stop" button that stops the audio and goes back to the beginning of the track currently being played. I thought about using the current playback postion somehow, but can't get my head around that.

我需要一个“停止”按钮来停止音频并返回到当前正在播放的曲目的开头。我想以某种方式使用当前的播放位置,但无法理解。

I've also tried this (adapted by advice here in another question), to no avail:

我也试过这个(根据另一个问题的建议改编),但无济于事:

`    function stop() {
        var audioPlayer = document.getElementsByTagName('audio');
        addEventListener('loadedmetadata', function() {
        this.currentTime = 0;
            }, false);
        }
`

Any ideas?

有任何想法吗?

回答by robertc

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

如果您的播放/暂停工作正常,这应该适用于停止:

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

回答by Yasser Shaikh

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

这是一个小的演示代码,您可以在其中播放、暂停和停止音频。

Live Demo

现场演示

HTML

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

来源

回答by Tony AS

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

我们在这里使用 java 更改歌曲 src 然后将其放回那里!棘手但有效