如何在未选中复选框时使用javascript关闭HTML5音频? [英] How to use javascript to turn off HTML5 audio when checkbox is unchecked?

查看:91
本文介绍了如何在未选中复选框时使用javascript关闭HTML5音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下javascript代码来播放一些HTML5声音:

  var html5_audiotypes = {//定义音频文件列表扩展及其相关的音频类型。如果您指定的音频文件不在此列表中,请添加到此目录:
mp3:audio / mpeg,
mp4:audio / mp4,
ogg :audio / ogg,
wav:audio / wav
}

函数createsound(sound){
var html5audio = document.createElement 'audio')
if(html5audio.canPlayType){//检查是否支持HTML5音频
for(var i = 0; i var sourceel = document .createElement('source')
sourceel.setAttribute('src',arguments [i])
if(arguments [i] .match(/ \。(\w +)$ / i) )
sourceel.setAttribute('type',html5_audiotypes [RegExp。$ 1])$ ​​b $ b 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 does not support HTML5 audio)}}
}
}

//初始化两个具有1个备用文件的声音片段:
var sound1 = createsound(audio / tileSelect.ogg,audio / tileSelect.mp3)
var sound2 = createsound /tileRemove.ogg,audio / tileRemove.mp3)

sound1.playclip();

$

问题:



我有一个ID为 sound 的复选框。



复选框的代码目前是:

  function playSound(){
if(document.getElementById('sound')。checked){
[ ]
}
}

我需要在此部分中添加在选中时听到声音,而在未选中时听到声音?



问候,
Maurice

解决方案

不介意,我解决了。我在想太硬,可以做得很简单:

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

这项工作


I have the following javascript code to play some HTML5 sounds:

    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")

With sound1.playclip(); used in other functions I can play the sounds

The problem:

I have a checkbox with ID: sound. When checked, the sound should be heard, if not, no sound should be heard.

The code for the checkbox currently is:

function playSound() {
    if (document.getElementById('sound').checked){
        [something has to be added here?]
    }
}

What do I need to add in this part to hear the sound when checked and not when unchecked? I can't seem to get it working.

Kind regards, Maurice

解决方案

Nevermind, I solved it. I was thinking too hard, it can be done very simple:

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

This works

这篇关于如何在未选中复选框时使用javascript关闭HTML5音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆