Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause streaming in soundcloud javascript api

I would like to allow a sound file to play and pause. The documentation shows how to play a streaming file:

    $("#stream").live("click", function(){      
          SC.stream("/tracks/{'track-id'}", function(sound){
             sound.play();
          };

It uses Sound Manager to stream and uses some of it's objects and methods... like .pause()!

So here's the code i think should work:

    var is_playing="false";

    $("#stream").live("click", function(){

        SC.stream("/tracks/{'track-id'}", function(sound){
            if (is_playing==="true")
                {
                    sound.pause();
                    is_playing = "false";
                    console.log(is_playing);
                }
            else if (is_playing === "false")
                {
                    sound.play();
                    is_playing = "true";
                    console.log(is_playing);
                }

            });

However, it continues to play the track without pausing. Any ideas? Thanks.

like image 976
bkbarton Avatar asked Aug 08 '26 19:08

bkbarton


2 Answers

var is_playing = false;
soundManager.setup({
    url: '/swf/soundmanager2.swf',
    preferFlash: false,
    onready: function() {
        var mySound = soundManager.createSound({
            id: 'aSound',
            url: 'sonidos/sonido1.mp3' ,
            autoplay: false
        });

        $("#stream").live("click", function(){    
            if (is_playing==false){
                    mySound.play();
                    is_playing = true;
                }else if (is_playing== true) {
                    mySound.stop();
                    is_playing = false;
             }
        });
    },
    /*    ontimeout: function() {
    // Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.?
    }*/
});
</script>
like image 152
Regulo Avatar answered Aug 11 '26 09:08

Regulo


You have to use sound.stop(); There is also a built in function to toggle play/pause of a sound. Use sound.togglePause(); to do that.

like image 21
kongshem Avatar answered Aug 11 '26 09:08

kongshem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!