繁体   English   中英

取消选中复选框时,如何使用JavaScript关闭HTML5音频?

[英]How to use javascript to turn off HTML5 audio when checkbox is unchecked?

我有以下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?]
    }
}

我需要在此部分中添加什么才能在选中时听到声音而在未选中时听到声音? 我似乎无法正常工作。

亲切的问候,莫里斯

没关系,我解决了。 我想得太辛苦了,可以很简单地完成:

if (document.getElementById('sound').checked){
                sound1.playclip(); 
            }

这有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM