Jquery Slider控制jquery音频播放器

时间:2015-11-28 15:42:31

标签: javascript jquery html audio

我正在研究jquery播放器的音频滑块,目标是让音频滑块控制音量。音频播放器是使用Jquery而不是音频标签启动的,并且具有类.play由于某种原因,滑块不会改变音量。

修改:工作解决方案 - http://jsfiddle.net/jeffd/2fjnmdkb/2/

JS小提琴:http://jsfiddle.net/jeffd/2fjnmdkb/1/

 $(".play").on('click', function () {
     var key = $(this).attr('key');
     EvalSound(this, key);
     var this_play = $(this);
     $(".play").each(function () {
         if ($(this)[0] != this_play[0]) {
             $(this).removeClass("pause");
         }
     });
     $(this).toggleClass("pause");
 });

 var thissound = new Audio();
 var currentKey;

 function EvalSound(el, key) {

     thissound.addEventListener('ended', function () {
         // done playing
         $(el).removeClass("pause");
     });

     if (currentKey !== key) thissound.src = "http://99centbeats.com/1e4cb5f584d055a0992385c1b2155786/" + key + ".mp3";
     currentKey = key;

     if (thissound.paused) thissound.play();
     else thissound.pause();
     thissound.currentTime = 0;
     currentPlayer = thissound;


 }
$(".volume_slider").slider({
    value  : 75,
    step   : 1,
    range  : 'min',
    min    : 0,
    max    : 100,
    slide  : function(){
        var value = $(".volume_slider").slider("value");
        $('.play').prop('volume', (value/100));
    }
});

1 个答案:

答案 0 :(得分:1)

我将.play选择器更改为var thissound并且它可以正常工作

工作Js小提琴:http://jsfiddle.net/jeffd/2fjnmdkb/2/

$(".play").on('click', function () {
     var key = $(this).attr('key');
     EvalSound(this, key);
     var this_play = $(this);
     $(".play").each(function () {
         if ($(this)[0] != this_play[0]) {
             $(this).removeClass("pause");
         }
     });
     $(this).toggleClass("pause");
 });

 var thissound = new Audio();
 var currentKey;

 function EvalSound(el, key) {

     thissound.addEventListener('ended', function () {
         // done playing
         $(el).removeClass("pause");
     });

     if (currentKey !== key) thissound.src = "http://99centbeats.com/1e4cb5f584d055a0992385c1b2155786/" + key + ".mp3";
     currentKey = key;

     if (thissound.paused) thissound.play();
     else thissound.pause();
     thissound.currentTime = 0;
     currentPlayer = thissound;


 }
$(".volume_slider").slider({
    value  : 75,
    step   : 1,
    range  : 'min',
    min    : 0,
    max    : 100,
    slide  : function(){
        var value = $(".volume_slider").slider("value");
        thissound.volume = (value / 100);
    }
});