【问题标题】:How to turn music on and off with one button?如何一键开启和关闭音乐?
【发布时间】:2013-12-19 14:51:40
【问题描述】:

对于我的网站,我使用 ION.sound 插件,我想暂停片段并使用相同的按钮再次播放。不幸的是,Jquery 的 .toggle 在 1.9 版本中已经被移除,不能再使用了。我怎么能用同一个按钮打开和关闭这个音频片段?这是我到目前为止所拥有的:

   $.ionSound({
         sounds: [
             "track_radio"
            ],
            path: "sounds/",
            multiPlay: true,
            volume: "0.8"
        });

    playRadio = function() {
      $.ionSound.play("track_radio");

    }

    stopRadio = function() {
      $.ionSound.stop("track_radio");
    }


    $("#speakers").click(function(event){
        playRadio();
    });

【问题讨论】:

    标签: javascript jquery audio toggle ionsound


    【解决方案1】:

    你需要一些东西来跟踪播放状态并在点击时检查它。

    $.ionSound({
         sounds: [
             "track_radio"
            ],
            path: "sounds/",
            multiPlay: true,
            volume: "0.8",
            playing: false;
        });
    
    onOffRadio = function() {
      $.ionSound.play("track_radio");
      $.ionSound.playing = true;
    }
    
    stopRadio = function() {
      $.ionSound.stop("track_radio");
      $.ionSound.playing = false;
    }
    
    
    $("#speakers").click(function(event){
        if ($.ionSound.playing) {
            stopRadio();
        }
        else {
            playRadio();
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以简单地利用按钮元素的“文本”属性。

      <button id="play-pause">Play</button>
      
      $("#play-pause").click(function() {        
           if($(this).text() == 'Play') {
              playRadio();
              $(this).text('Pause');
           }
           else {
              pauseRadio();
              $(this).text('Play'); 
           }
      });
      

      Here's the fiddle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-05
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 2010-09-11
        • 2010-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多