简体   繁体   中英

How to turn music on and off with one button?

For my website I use the ION.sound plugin and i would like to pause a fragment and play it again with the same button. Unfortunately, Jquery's .toggle has been removed in version 1.9 and can not be used anymore. How could I turn this audiofragment on and off with the same button? This is what i have so far:

   $.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();
    });

You need something to track the playing status and check it when clicked.

$.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();
    }
});

You can simply make use of the 'text' attribute of the button element.

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM