我的JavaScript音量滑块无法正常工作

时间:2017-11-03 21:34:43

标签: javascript jquery

我的音量按钮和音量滑块没有问题,它无法正常工作。所以,我正在寻找帮助来解决这个问题。

代码如下:

var audio,  mutebtn,  volumeslider, seeking=false, seekto;

function initAudioPlayer(){     audio = document.getElementById(" myTune");

// Set object references
mutebtn = document.getElementById("mutebtn");
volumeslider = document.getElementById("volumeslider");
// Add Event Handling

mutebtn.addEventListener("click", mute);
seekslider.addEventListener("mousedown", function(event){ seeking=true; seek(event); });

volumeslider.addEventListener("mousemove", setvolume);
// Functions

function mute(){
    if(audio.muted){
        audio.muted = false;
        mutebtn.style.background = "url(http://iflixweb.stream/radio.co-player/spk.png) no-repeat";
    } else {
        audio.muted = true;
        mutebtn.style.background = "url(http://iflixweb.stream/radio.co-player/mute.png) no-repeat";
    }
}
function seek(event){
    if(seeking){
        seekslider.value = event.clientX - seekslider.offsetLeft;
        seekto = audio.duration * (seekslider.value / 100);
        audio.currentTime = seekto;
    }
  }
function setvolume(){
    audio.volume = volumeslider.value / 100;
  }

}   window.addEventListener(" load",initAudioPlayer);

1 个答案:

答案 0 :(得分:0)

更改此部分:

audio = new Audio();

成:

audio = document.getElementById("myTune");

此外,您的seeklider未定义,所以现在,您可以评论这些行,直到您实现它。

// seekslider.addEventListener("mousedown", function(event){ seeking=true; seek(event); });
// seekslider.addEventListener("mousemove", function(event){ seek(event); });
// seekslider.addEventListener("mouseup",function(){ seeking=false; });

这是一个功能性的代码:

https://codepen.io/anon/pen/BmKLge

编辑:从新的代码笔中注释掉第11行。

//seekslider.addEventListener("mousedown", function(event){ seeking=true; seek(event); });

要让您的静音音量按您的方式工作,请添加

function setvolume(){
        audio.volume = volumeslider.value / 100;
        if (!audio.volume) {
            mutebtn.style.background = "url(http://iflixweb.stream/radio.co-player/mute.png) no-repeat";
        } else {
        mutebtn.style.background = "url(http://iflixweb.stream/radio.co-player/spk.png) no-repeat";
        }
}