Soundcloud js api. Can't stop playing

652 views Asked by At

I'm developing app to play soundcloud traks. I'm using ttwmusicplayer and want to bind soundcloud actions on it's play/pause. So, I'm doing it like this:

$("#mainPlayer .jp-play")
        .off("click")
        .on("click", function(){
            $("#mainPlayer .jp-play").css('display','none');
            $("#mainPlayer .jp-pause").css('display','block');
            SC.stream('/tracks/208818225', function(sound){
                sound.play();
            });
        });
$("#mainPlayer .jp-pause")
    .off("click")
    .on("click", function(){
            alert('IT WORKS');
            $("#mainPlayer .jp-play").css('display','block');
            $("#mainPlayer .jp-pause").css('display','none');
            SC.stream('/tracks/208818225', function(sound){
                sound.stop();
            });
        });

It plays track fine,but when I click pause button, I receive alert, so I can understand that this works. Also pause changes itself on play, but music still playing, however I found this methond in docs How can I pause/stop soundcloud tracks then?

2

There are 2 answers

1
Jawad Khan On BEST ANSWER

I think there should be one sound object

     var mysound; 
     SC.stream('/tracks/208818225', function(sound){
          mysound = sound;
          mysound.play();
     });

and to stop sound no need to use SC.stream instead just use the sound object. like:

    mysound.stop() //or mysound.pause()
0
spaceofsn On

To pause:

mysound.pause();

To stop:

mysound.pause();
mysound.seek(0);

I couldn't find any stop() methods in the documentation, so I think you have to pause() first, then seek() to the beginning to "stop" the sound. https://developers.soundcloud.com/docs/api/sdks#javascript