1

我正在使用此站点上发布的以下脚本:http ://www.javascriptkit.com/script/...oundlink.shtml

它工作得很好,但我想扩展这个脚本并使用复选框使声音静音。此复选框具有 id: sound。如果选中,我想听到声音,如果未选中,则不应听到声音。

这是我目前用于复选框的代码:代码:

function playSound() {
    if (document.getElementById('sound').checked){
        -something needs to be added here to make it work?-
    }
}

任何人都知道如何使这项工作?

非常感谢

4

2 回答 2

1

功能 muteIt(box) { if (clicksound){ clicksound.muted = box.checked?true:false; } if (mouseoversound){ mouseoversound.muted = box.checked?true:false; } }

于 2012-04-30T19:39:56.217 回答
0
<script>

// Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com)
// Visit JavaScript Kit at http://www.javascriptkit.com/ for full source code

//** Usage: Instantiate script by calling: var uniquevar=createsoundbite("soundfile1", "fallbackfile2", "fallebacksound3", etc)
//** Call: uniquevar.playclip() to play sound   

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 createsoundbite(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(){
                    if (document.getElementById('sound').checked){

                html5audio.pause()
                    html5audio.currentTime=0
                    html5audio.play()

                    }
        }
    return html5audio
}
else{
    return {playclip:function(){throw new Error("Your browser doesn't support    HTML5 audio unfortunately")}}
}
}

//Initialize two sound clips with 1 fallback file each:

var mouseoversound=createsoundbite("whistle.ogg", "whistle.mp3")
var clicksound=createsoundbite("click.ogg", "click.mp3")

</script>

因此,它会检查该框是否被选中。如果是,那么它会播放。如果没有,则不会播放。

于 2012-04-30T19:45:20.340 回答