我想製作一個簡單的按鈕來打開和關閉我的音頻文件。使用香草的單個按鈕切換音頻javascript
一旦我點擊播放,我無法關閉它。
我使用圖像作爲我的按鈕,並使用div.onclick
來觸發此功能。
html audio
標記有ID audioPlay
。
我使用全局布爾值playSound
,最初設置爲false
。
我的劇本是這樣的:
var playSound = false;
if (playSound != true) {
audioButton.src = '../../testStuff/audioPlay.png';
audio.onclick = function() {
document.getElementById('audioPlay').play();
playSound = true;
console.log(playSound);
}
}
if (playSound == true) {
audioButton.src = '../../testStuff/audioStop.png';
audio.onclick = function() {
document.getElementById('audioPlay').pause();
playSound = false;
}
}
當我點擊它,第一次它工作正常。它將playSound
設置爲true
。
但是,當我第二次點擊它時,沒有任何反應。有些東西是playSound
回到false
,我不知道爲什麼。
我已經嘗試切換==true if
聲明false
一個以上,以及兩個if
語句擀成一個單一的onclick
功能,但它仍然經營這種方式。
任何想法?
這是整個代碼塊?你在事件處理程序中發佈了什麼? –
你使用html5音頻標籤來播放音頻嗎?看看這個原型的來源:http://www.scottandrew.com/pub/html5audioplayer/ – visc
我沒有使用音頻播放器只是