2013-07-14 3 views
5

私はJavascriptとSoundcloud SDKを初めて使用しています。私の現在の解決策が基本的なものではない場合は、どう改善できるか教えてください。ストリーミングのためにSoundcloud Javascript SDKを使用して、前のトラックが完了した後、自動的に次のサウンドに移動できますか?

私はカスタムサウンドクラウドプレーヤーを構築しており、事前ビルドされたウィジェットは使用していません。私はを自動的に探していますトラックが終了した後に次のトラックに移動します。私は、Soundcloudプレイリストを使用せずにこれを達成できるようにしたいと思います。代わりに、私は再生するトラックのJSONのリストを引っ張っていきます。

リンクをクリックしてトラックを再生、一時停止、停止、スキップすることはできますが、nextTrack機能を起動するためにトラックの再生が完了したときの通知方法がわかりません。助言がありますか?

てSoundcloudのJavascript SDKストリーミングAPI:ここ http://developers.soundcloud.com/docs/api/sdks#streaming

は私のコードです:

<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":"00"},{"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> 

答えて

7

トラックオブジェクトを作成するときには、今あなたが機能してコンソール出力を置き換えることができ、onfinishメソッドを実装することができます。

SC.stream("http://api.soundcloud.com/tracks/" + trackId, {onfinish: function(){ 
    console.log('track finished'); 
}}, function(sound){ 
    currentTrack = sound; 
}); 
+0

BNZおかげで - 私は "機能(音){currentTrack =音が;}" とは何をん:) – jmejia

+0

を必要と正確に何をすべきか? –

+0

私はこれを試しましたが、コンソールを持っていません...確かにそれは動作しません。 – Francesco

関連する問題