Play playlist or track by permalink not trackid

狂风中的少年 提交于 2019-12-13 03:24:19

问题


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.


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


来源:https://stackoverflow.com/questions/19351797/play-playlist-or-track-by-permalink-not-trackid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!