简体   繁体   English

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

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

I have the following javascript code to play some HTML5 sounds: 我有以下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")

With sound1.playclip(); 使用sound1.playclip(); used in other functions I can play the sounds 用于其他功能我可以播放声音

The problem: 问题:

I have a checkbox with ID: sound . 我有一个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 这有效

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

相关问题 使用 HTML5 / javascript 校正音频音量输出分贝,关闭预处理 - Correct audio volume output decibels using HTML5 / javascript, turn off pre-processing HTML5 视频:如何关闭字幕 - HTML5 video: how to turn off subtitles 当在 JavaScript 中取消选中复选框时,在屏幕外创建一个 div go - make a div go off screen when a checkbox is unchecked in JavaScript 如何使用javascript自动播放html5音频标签 - How to use a javascript to autoplay a html5 audio tag 当屏幕关闭时,HTML5音频/ javascript停止在android(谷歌浏览器)上工作 - HTML5 audio/javascript stops working on android(google chrome) when screen is turned off 当html id是php变量时,如何在选中复选框时添加并在Javascript中未选中时扣除 - How to add when a checkbox is checked and deduct when unchecked in Javascript when html id is a php variable 在JavaScript中使用HTML5音频标签 - Use of HTML5 audio tags with JavaScript 使用html5音频中的文本链接来打开/关闭javascript执行 - Toggle javascript execution on/off with text link in html5 audio 在HTML5和JavaScript中加载音频时播放 - Play audio when it is loaded in HTML5 and JavaScript 当 Html 复选框未选中时,有什么方法可以触发 Javascript 中的事件? - Any way of triggering a event in Javascript when Html checkbox gets unchecked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM