我有一种使用简单命令播放声音的方法,https://github.com/admsev/jquery-play-sound,但我正在播放的音频文件大约 2 分钟长。我需要一种使用 javascript 或 jquery 命令停止/静音音频的方法。有谁知道我该怎么做?我真的不想要一个停止声音的按钮,除非它是一个常规的 html 按钮,除了停止声音之外还可以做其他事情。
问问题
481 次
2 回答
1
您链接的jQuery playSound库不支持暂停。我认为最好选择一个支持暂停的不同库,这样您就不必自己编写该功能。
我之前使用过howler.js库,它对我来说很好用。以下是使用 howler.js 的方法:
<!-- to load the library on the page -->
<script src="howler.js">
// to create and play the sound
var backgroundMusic = new Howl({
urls: ['music.mp3', 'music.ogg'],
}).play();
// to pause the sound
// (make sure the variable `backgroundMusic` is accessible)
backgroundMusic.pause();
// to stop the sound
backgroundMusic.stop();
于 2016-05-15T01:11:18.473 回答
1
没有 jQuery,没有插件,只有一个浏览器。这是一个播放和暂停 MP3 的按钮。你没有指定你想让一个按钮做哪些额外的事情,所以我没有为那个单独的按钮添加任何额外的功能。你还想让这个单独的按钮做什么?
顺便说一句,如果您希望音频停止而不是暂停:
块中有一行
<style>
需要取消注释,还有一行需要注释。块中有一行
<script>
需要取消注释。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.play:before {
content: '\25b6';
}
.pause:before {
content: '\275a\275a'; /*Uncomment this line for pause */
/* content: '\25a0'; */ /*Uncomment this line for stop */
}
</style>
</head>
<body>
<button id="button" class="play">
<audio id="audio" src="http://glpjt.s3.amazonaws.com/so/av/pf-righteous.mp3"></audio>
</button>
<script>
var audio = document.getElementById('audio');
var button = document.getElementById('button');
button.addEventListener('click', playPause, false);
function playPause() {
if (!audio.paused) {
audio.pause();
// audio.currentTime = 0; // Uncomment this line for stop
button.classList.remove('pause');
button.classList.add('play');
} else {
audio.play();
button.classList.remove('play');
button.classList.add('pause');
}
}
</script>
</body>
</html>
于 2016-05-15T01:31:59.257 回答