我有歌曲列表,即时通讯使用的方式是:
<a href="path to song 1">play</a>
<a href="path to song 2">play</a>
...
当链接被点击时,这首歌开始了,我用jquery做到了,就像这样:
$('a').on('click', function() {
var path = $(this).attr('href');
var song = new Audio(path);
song.play();
});
单击链接后如何进行播放/停止切换?
最佳答案
像这样切换play()
和pause()
函数:
var isPlaying = false
$("a").on("click", function(e) {
e.preventDefault();
var path = $(this).attr('href');
var song = new Audio(path);
if (!isPlaying) {
isPlaying = true;
song.play();
$(this).text("Pause")
} else {
isPlaying = false;
song.pause();
$(this).text("Play");
}
console.log(isPlaying);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="path to song 1">play</a>
<a href="path to song 2">play</a>
关于jquery - jQuery播放停止音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48746270/