SoundManager reset SoundCloud playlist onfinish

161 views Asked by At

This is my interpretation of a custom Soundcloud player using JavaScript SDK. I would like to reset playlist after last track is finished, which is partially acheved - 1st track is properly set, but my counter variable for next/prev buttons doesn't update.

Or maybe there is a better way to loop through the playlist than having a counter variable?

var autoplay = false;

$(document).ready(function() {

  SC.get( '/resolve', {url: 'https://soundcloud.com/rockcurrent/sets/rc-playlist-new-rock-releases'}, function(pl) {

    var i = 0;
    var track_url = pl.tracks[i].uri;

    var stream_sound = function(i) {
      track_url = pl.tracks[i].uri;

      SC.stream(track_url, function(sound) {

        var new_play = function() {
          sound.play({
            onplay: function() {
              title(i); 
            },    
            whileplaying: function() {
              loader(this.position, this.duration);             
            },
            onfinish: function() {
              if (i < pl.tracks.length -1) {
                i++;
                autoplay = true;             
              }
              else {
                i = 0;
              }
              stream_sound(i); 
            }
          });
        }

        new_play();
        sound.pause();

        if (autoplay) {
          sound.resume();
          is_playing = true;
        }

        autoplay = false;

        $('#play').on('click', function() {
          togglePause();     
        });

      });
}

stream_sound(i);

$('#next').on('click', function() {
  soundManager.stopAll();
  if (i < pl.tracks.length -1) {
    i++;
  }
  autoplay = true;
  stream_sound(i);
});

$('#prev').on('click', function() {
  soundManager.stopAll();
  if (i > 0) {
    i--;
  }
  autoplay = true;
  stream_sound(i);
});

}); 
});
0

There are 0 answers