使用Soundcloud Javascript SDK进行流式处理后,如何在上一首曲目完成后自动移动到下一个声音?
我是Javascript和SoundCloudSDK的新手,所以如果我当前的解决方案离基础太远,请告诉我如何改进 我正在构建一个定制的Soundcloud播放器,而不是使用预先构建的小部件。我希望在一首曲目播放完毕后自动移动到下一首曲目。我希望能够在不使用Soundcloud播放列表的情况下实现这一点。相反,我将拉入一个JSON曲目列表来播放 我可以通过单击链接播放、暂停、停止和跳过曲目,但我不知道如何判断曲目何时已完成播放,以触发使用Soundcloud Javascript SDK进行流式处理后,如何在上一首曲目完成后自动移动到下一个声音?,javascript,soundcloud,Javascript,Soundcloud,我是Javascript和SoundCloudSDK的新手,所以如果我当前的解决方案离基础太远,请告诉我如何改进 我正在构建一个定制的Soundcloud播放器,而不是使用预先构建的小部件。我希望在一首曲目播放完毕后自动移动到下一首曲目。我希望能够在不使用Soundcloud播放列表的情况下实现这一点。相反,我将拉入一个JSON曲目列表来播放 我可以通过单击链接播放、暂停、停止和跳过曲目,但我不知道如何判断曲目何时已完成播放,以触发nextTrack功能。有什么建议吗 Soundcloud Ja
nextTrack
功能。有什么建议吗
Soundcloud Javascript SDK流式API:
这是我的密码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://connect.soundcloud.com/sdk.js"></script>
<div class="music-player">
<h4 class="trackTitle">Current track</h4>
<a href="#" id="play">Play</a>
<a href="#" id="pause" style="display:none;">Pause</a>
<a href="#" id="stop">Stop</a>
<a href="#" id="next">Next</a>
</div>
<script>
Track = function (trackId){
var currentTrack = "";
SC.initialize({
client_id: "CLIENT_ID"
});
SC.stream("http://api.soundcloud.com/tracks/" + trackId, function(sound){
currentTrack = sound;
});
this.play = function() {
currentTrack.play();
};
this.pause = function() {
currentTrack.pause();
};
this.stop = function() {
currentTrack.stop();
};
};
Rotation = function(tracks) {
var currentTrack = tracks[0];
this.currentTrack = function () {
return currentTrack;
};
this.nextTrack = function () {
var currentIndex = tracks.indexOf(currentTrack);
var nextTrackIndex = currentIndex + 1;
var nextTrackId = tracks[nextTrackIndex];
currentTrack = nextTrackId;
return currentTrack
};
};
$(document).ready (function(){
var songs = [{"title":"Sad Trombone","song_url":"https://soundcloud.com/sheckylovejoy/sad- trombone","soundcloud_id":"18321000"},{"title":"AraabMUZIK - \"Beauty\"","song_url":" https://soundcloud.com/selftitledmag/araabmuzik-beauty","soundcloud_id":"79408289"}]
var rotation = new Rotation(songs);
var currentTrack = rotation.currentTrack();
var currentPlayingTrack = new Track(currentTrack.soundcloud_id);
$('#play').on('click', function(event){
currentPlayingTrack.play();
$('.trackTitle').html(currentTrack.title);
$('#pause').show();
$('#play').hide();
});
$('#pause').on('click', function(event){
currentPlayingTrack.pause();
$('#pause').hide();
$('#play').show();
});
$('#stop').on('click', function(event){
currentPlayingTrack.stop();
$('#pause').hide();
$('#play').show();
});
$('#next').on('click', function(event){
currentPlayingTrack.stop();
currentTrack = rotation.nextTrack();
currentPlayingTrack = new Track(currentTrack.soundcloud_id);
currentPlayingTrack.play();
$('.trackTitle').html(currentTrack.title);
});
});
</script>
当前轨道
轨迹=功能(轨迹ID){
var currentTrack=“”;
SC.initialize({
客户id:“客户id”
});
SC.stream(“http://api.soundcloud.com/tracks/“+trackId,功能(声音){
currentTrack=声音;
});
this.play=函数(){
currentTrack.play();
};
this.pause=函数(){
currentTrack.pause();
};
this.stop=函数(){
currentTrack.stop();
};
};
旋转=功能(轨道){
var currentTrack=磁道[0];
this.currentTrack=函数(){
返回电流轨迹;
};
this.nextTrack=函数(){
var currentIndex=tracks.indexOf(currentTrack);
var nextTrackIndex=currentIndex+1;
var nextTrackId=轨道[nextTrackIndex];
currentTrack=nextTrackId;
回流轨道
};
};
$(文档).ready(函数(){
var songs=[{“标题”:“悲伤长号”,“歌曲url”:https://soundcloud.com/sheckylovejoy/sad- 长号,“soundcloud\u id:“18321000”},{“title:“Arabmuzik-“Beauty\”,“song\u url:”https://soundcloud.com/selftitledmag/araabmuzik-beauty,“soundcloud_id”:“79408289”}]
var旋转=新旋转(歌曲);
var currentTrack=rotation.currentTrack();
var currentPlayingTrack=新曲目(currentTrack.soundcloud\u id);
$(“#播放”)。在('click',函数(事件){
currentPlayingTrack.play();
$('.trackTitle').html(currentTrack.title);
$(“#暂停”).show();
$(“#播放”).hide();
});
$(“#暂停”)。在('click',函数(事件){
currentPlayingTrack.pause();
$(“#暂停”).hide();
$('#play').show();
});
$(“#停止”)。在('click',函数(事件){
currentPlayingTrack.stop();
$(“#暂停”).hide();
$('#play').show();
});
$(“#下一步”)。在('click',函数(事件){
currentPlayingTrack.stop();
currentTrack=rotation.nextTrack();
currentPlayingTrack=新曲目(currentTrack.soundcloud\u id);
currentPlayingTrack.play();
$('.trackTitle').html(currentTrack.title);
});
});
您可以在创建轨迹对象时实现onfinish方法,现在可以用函数替换控制台输出
SC.stream("http://api.soundcloud.com/tracks/" + trackId, {onfinish: function(){
console.log('track finished');
}}, function(sound){
currentTrack = sound;
});
感谢bnz——这正是我需要的:)函数(声音){currentTrack=sound;}做什么?