想要在全屏视频背景上为音频创建自定义静音/取消静音按钮

时间:2015-01-16 15:30:28

标签: javascript html5 audio video controls

我目前有一个全屏幕背景视频,我已经循环播放音频,我想制作一个自定义按钮,以便访问者访问该网站时可以将音频静音/取消静音。

我尝试过使用此代码......

<video id="bg" loop autoplay preload="auto" poster="../img/still.jpg">
   <source src="vid/vidbg2.mp4" type="video/mp4" />
   <source src="vid/vidbg.webm" type="video/webm" />
   <source src="vid/vidbg.ogv" type="video/ogg" />
   Your browser does not support this video
</video>
<div id="video_controls">
   <button id="mutebtn">mute</button>
</div> 

和JavaScript

<script>
    var mutebtn;
    function intitializePlayer(){
    //set object references
    vid = document.getElementById("bg");
    mutebtn = document.getElementById("mutebtn");
    //add event listener
    mutebtn.addEventListener("click,vidmute,false");
  }
    function.vidmute(){
    if(vid.muted){
    vid.muted = false;
    mutebtn.innerHTML = "mute";
} else {
    vid.muted = true;
    mutebtn.innerHTML = "Unmute";
}
}
</script>

想想我可能会咆哮错误的树?

任何帮助都会非常感谢!

1 个答案:

答案 0 :(得分:0)

在需要的地方看起来像三个小的变化 1 /仅引用click而不是整个addEventListener参数 2 /调用初始化 3 /从function.vidmute

中删除额外的期间
<script>
var mutebtn;
intitializePlayer()  // **don't forget to call the initialize**

function intitializePlayer(){
    //set object references
    vid = document.getElementById("bg");
    mutebtn = document.getElementById("mutebtn");
    //add event listener
    mutebtn.addEventListener("click",vidmute,false);   // **quotes only around the event** 
}

function vidmute(){   // **there was an extra '.' between function and vidmute**
    if(vid.muted){
        vid.muted = false;
        mutebtn.innerHTML = "mute";
    } else {
        vid.muted = true;
        mutebtn.innerHTML = "Unmute";
    }
}
</script>