javascriptsoundcloudsoundmanager2

Javascript: Setting track position form soundcloud api


I've been trying to play a track from Soundcloud at a specific position. I have used the code from the following question as a reference

Soundcloud API: how to play only a part of a track?

below is the code that I have used

function playTrack(id) {
  SC.whenStreamingReady(function() {
    var sound = SC.stream(id);
    sound.setPosition(240000); // position, measured in milliseconds
    console.log(sound.position)
    sound.play()
  });
}

The console.log returns 0 and the track plays from the beginning.

alternative code I have tried and had the same result with is below

SC.stream("/tracks/" + id, function(sound){
  console.log("playing")
  console.log(sound)
  sound.setPosition(120000)
  console.log(sound.position)
  sound.play()
})

Solution

  • Since soundcloud no longer uses soundmanager. the above methods will no longer work.

    What must be done is to use a function called "seek()" instead of "setPosition()"

    sound.seek(1500);
    

    Here's a better example:

      SC.stream(
        "/tracks/[trackID]",
        function(sound){
          sound.seek(1500)
      });
    

    And if you wanted to access the seek function outside of the callback I would do this:

      var soundTrack;
      SC.stream(
        "/tracks/[trackID]",
        function(sound){
          soundTrack = sound;
      });
    
      var buttonClickEvent = function(){
        soundTrack.seek(1500);
      }