SoundManager重置SoundCloud播放列表onfinish

时间:2015-05-10 11:07:19

标签: javascript soundcloud soundmanager2 playlists

这是我对使用JavaScript SDK的自定义Soundcloud播放器的解释。我希望在最后一首曲目完成后重置播放列表,这部分是有效的 - 第一首曲目已正确设置,但我的下一首/上一级按钮的计数器变量不会更新。

或许有更好的方法来循环播放列表而不是使用计数器变量?

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 个答案:

没有答案