我想制作一个简单的按钮来打开和关闭我的音频文件。使用香草的单个按钮切换音频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
我没有使用音频播放器只是