Question

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()
})
Was it helpful?

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);
  }

OTHER TIPS

According to SoundCloud's docs they are using SoundManager2 to handle playback, so you should be able to use any of its methods or settings. Try this:

function playTrack(id) {
  SC.whenStreamingReady(function() {
    var sound = SC.stream(id, {
      from: 120000, // position to start playback within a sound (msec)
      autoLoad: true,
      autoPlay: true,
      onplay: function(){ console.log(this.position) }
    });
  });
}

If that doesn't work you can try this instead:

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

This should work-

var play_sound = function(id) {

    SC.whenStreamingReady(function() {
        var sound = SC.stream("/tracks/"+id,{autoPlay: false}, function(sound) {
            sound.play({
                whileplaying: function() {
                    console.log( this.position );
                }
            });
        });
    });

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top