如何连续不停地播放所有喜爱的歌曲?
我写了一个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);
});
}
答案 0 :(得分:2)
我找到了解决方案。我使用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);
});
}