我有以下 javascript 代码来播放一些 HTML5 声音:
var html5_audiotypes={ //define list of audio file extensions and their associated audio types. Add to it if your specified audio file isn't on this list:
"mp3": "audio/mpeg",
"mp4": "audio/mp4",
"ogg": "audio/ogg",
"wav": "audio/wav"
}
function createsound(sound){
var html5audio=document.createElement('audio')
if (html5audio.canPlayType){ //check support for HTML5 audio
for (var i=0; i<arguments.length; i++){
var sourceel=document.createElement('source')
sourceel.setAttribute('src', arguments[i])
if (arguments[i].match(/\.(\w+)$/i))
sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
html5audio.appendChild(sourceel)
}
html5audio.load()
html5audio.playclip=function(){
html5audio.pause()
html5audio.currentTime=0
html5audio.play()
}
return html5audio
}
else{
return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio")}}
}
}
//Initialize two sound clips with 1 fallback file each:
var sound1=createsound("audio/tileSelect.ogg", "audio/tileSelect.mp3")
var sound2=createsound("audio/tileRemove.ogg", "audio/tileRemove.mp3")
在sound1.playclip();
其他功能中使用时,我可以播放声音
问题:
我有一个带有 ID: 的复选框sound
。检查时,应听到声音,如果没有,则不应听到声音。
当前复选框的代码是:
function playSound() {
if (document.getElementById('sound').checked){
[something has to be added here?]
}
}
我需要在这部分添加什么才能在选中而不是未选中时听到声音?我似乎无法让它工作。
亲切的问候,莫里斯