Soundcloud SDK 3. 最喜欢的(喜欢的)歌曲的不间断流

Soundcloud SDK 3. Non stop stream of favourite (liked) songs

如何不间断地连续播放所有喜爱的歌曲?

我写了一个 js 代码来获取我最喜欢的歌曲,随机播放一首。第一首歌曲播放完毕后播放下一首随机歌曲。但我的目标是永远一首一首地播放所有歌曲,直到我想手动停止它为止。

我的代码:

var getRandom = function(min, max){
    return Math.round(Math.random() * (max - min) + min);
}

function playFon() {
    SC.connect().then(function(){
    SC.get('/me').then(function(me) { 
        SC.get('/users/'+ me.id +'/favorites?limit=200').then(function(data) { 
                var num_of_res = data.length;
                var sid = getRandom(0, num_of_res);
                var song = '/tracks/' + data[sid].id;

                SC.stream(song).then(function(player){

                    player.on('play-start', function(){
                        console.log('Playing '+ sid + ' ' + song);  
                        console.log(data[sid]);
                    }).on('finish', function(){
                        sid = getRandom(0, num_of_res);
                        song = '/tracks/' + data[sid].id;

                        console.log('Playing '+ sid + ' ' + song);  
                        console.log(data[sid]);

                        SC.stream(song).then(function(player){
                            player.play();
                        });
                    });

                    player.play();

                });
        });
        });
    }).then(function(data){
      console.log('You are playing now.');
    }).catch(function(error){
      console.log('Error: ' + error.message);
    });

}

我找到了解决办法。我使用 SoundManager2 和递归函数 nextTrack() 播放下一个随机曲目。我觉得也可以通过SoundCloud播放器来完成。

var fav_songs_arr = [];
var num_of_res = 0;
var sid = 0;
var url = '';

var getRandom = function(min, max){
    return Math.round(Math.random() * (max - min) + min);
}

var nextTrack = function(){
    soundManager.stopAll();
    sid = getRandom(0, num_of_res);
    url = fav_songs_arr[sid].stream_url + '?consumer_key=' + app_id;
    var mySound = soundManager.createSound({
        url: url,
        onfinish: function(){
            nextTrack();
        }
    });
    mySound.play();
}

function playFon() {
    SC.connect().then(function(){
      SC.get('/me').then(function(me) { 
        SC.get('/users/'+ me.id +'/favorites?limit=200').then(function(data) { 
                fav_songs_arr = data;
                num_of_res = fav_songs_arr.length;
                nextTrack();
          });
        });
    }).then(function(data){
      console.log('You are playing now.');
    }).catch(function(error){
      console.log('Error: ' + error.message);
    });

}