0

I have an embedded player that works by trackId but:

  1. I but I am trying to get non-technical users to embed the tracks or playlists into pages.
  2. I sometimes need to regenerate the playlists

For both these reasons it is inconvenient that the player needs trackId. Is there a way I can play off the permalink?

Thanks, Martin.

PS. My current iframe code looks like this:

src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F$paramPlaylistID"

This iFrame code (from Comment #1) doesn't work:

src="https://w.soundcloud.com/player/?url=http://api.soundcloud.com/resolve.json?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F$paramPermalink"

Is there something better I can use? I recognize that I may need to do something to the url=...url= but I'm not clear whether resolve works with this player or whether the answer only applies to if I'd made a custom player.

Thanks again, M.

idbehold
  • 16,833
  • 5
  • 47
  • 74
Martin Cleaver
  • 909
  • 8
  • 21

1 Answers1

1

See SoundCloud's API docs on resolve:

The resolve resource allows you to lookup and access API resources when you only know the SoundCloud.com URL.

$ curl -v 'http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=YOUR_CLIENT_ID'

< HTTP/1.1 302 Moved Temporarily
< Location: http://api.soundcloud.com/tracks/49931.json

This request will resolve and redirect to the API resource URL for the track http://soundcloud.com/matas/hobnotropic. Just follow the redirect and you will get the representation you want. The resolver supports URLs for:

  • users
  • tracks
  • playlists (sets)
  • groups
  • apps

You can take a the permalink from a user and get the track or playlist ID like so:

function getSoundCloudId(permalink, callback) {
  var resolve = 'http://api.soundcloud.com/resolve.json?client_id=YOUR_CLIENT_ID&url=';
  var request = new XMLHttpRequest();
  request.onreadystatechange = function(){
    if (this.readyState === 4 && this.responseText) {
      var response = JSON.parse(this.responseText);
      if (response.id) callback(response.id);
    }
  };
  request.open('get', resolve+permalink, true);
  request.send();
}

Then you use it like so:

getSoundCloudId('https://soundcloud.com/matas/sets/library-project', function(id){
  // Do stuff with the ID
  console.log(id);
})
idbehold
  • 16,833
  • 5
  • 47
  • 74
  • Ok, so I've moved the iframe code above as I'm too limited in this box. Suffice to say that I am not really using the API in this case, just the embedded player in an iFrame. Is this still workable? Can you give me an example? Thanks kindly. M. – Martin Cleaver Oct 14 '13 at 02:44
  • Thanks @idbehold - so, will that work with w.soundcloud.com/player? – Martin Cleaver Oct 14 '13 at 16:10
  • @MartinCleaver I've created a [fiddle](http://jsfiddle.net/Z6tbj/) for you. Paste in a playlist/set permalink and click "Get Embed Code" to see it in action. – idbehold Oct 14 '13 at 18:56
  • Thanks, I really appreciate that! Looks good - now just waiting for my colleague to test it. Best, M. – Martin Cleaver Oct 15 '13 at 14:19
  • @idebehold - actually we do have a further issue on http://stackoverflow.com/questions/19440040/soundcloud-api-origin-mysite-is-not-allowed-by-access-control-allow-origin - thanks, M. – Martin Cleaver Oct 18 '13 at 01:21