簡単なコマンドhttps://github.com/admsev/jquery-play-soundでサウンドを再生する方法がありますが、再生しているオーディオ ファイルの長さは約 2 分です。javascript または jquery コマンドを使用して音声を停止/無音にする方法が必要です。どうすればこれができるか知っている人はいますか?サウンドを停止する以外に他のことを実行できる通常の html ボタンでない限り、サウンドを停止するボタンは本当に必要ありません。
2 に答える
1
jQuery やプラグインはなく、ブラウザだけです。これは、1 つの MP3 を再生および一時停止する 1 つのボタンです。1 つのボタンに何を追加するかを指定しなかったので、その 1 つのボタンに追加機能を追加しませんでした。この 1 つのボタンに他に何をしてもらいたいですか?
ところで、オーディオを一時停止ではなく停止したい場合は、次のようにします。
ブロックには、
<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 に答える
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 に答える