Py学习  »  Jquery

如何停止使用jquery播放音频?

Elvinci Chen • 5 年前 • 1531 次点击  

我想停止音频播放,但不仅要暂停,而且我也想如果我重新打开它,它开始在第一个,而不是中间我停止了它。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50745
 
1531 次点击  
文章 [ 2 ]  |  最新文章 5 年前
André DS
Reply   •   1 楼
André DS    6 年前

你已经试过什么了?这应该符合你的要求。

function stop(el){
    el.pause(); // Stop playing
    el.currentTime = 0; // Reset time
}

下面是一个工作示例 id 指定而不是 element

function stop(elementId) {
  var el = document.getElementById(elementId);
  el.pause(); // Stop playing
  el.currentTime = 0; // Reset time
}
<audio id="myAudio" controls>
  <source src="http://ccrma.stanford.edu/~jos/mp3/viola.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<br/><br/>
<button onclick="stop('myAudio')">Stop playing</button>
Rory McCrossan
Reply   •   2 楼
Rory McCrossan    6 年前

不能通过jQuery对象访问媒体控件。因此,您需要对元素对象本身调用与音频相关的方法。

使用 play() pause() 很明显,但是要将时间索引设置回文件的开头,需要使用 currentTime :

var audio = $('audio')[0]; // alternative: $('audio').get(0);

audio.pause();
audio.currentTime = 0;
audio.play();