Retrieve Soundcloud search results and embed results

夙愿已清 提交于 2020-01-24 20:52:51

问题


With Soundcloud, I've been looking at the API, but I'm not quite sure of the best way to do what I want.

I'm building a site which I'll use to host a number of sound files on Soundcloud. I'd like to be able to let people search for sounds (they'll be sermon mp3 files for a church website) and then list the results on our site, together with the embedded player.

What would be the best approach to use to display a player for each of the returned search results? Obviously, speed is a concern for us - so would like to approach this the best way.

Just a general approach would be fine - and if there is an example line of code or two, that would be awesome. I'm using PHP.

Thanks for any help,

Toby.


回答1:


General approach could be the following:

  1. Get search query from user
  2. Issue a request to http://api.soundcloud.com/tracks.json?q=YOUR_QUERY&client_id=YOUR_CLIENT_ID*
  3. Get JSON that will contain an array of tracks that you can iterate on and access properties, like sound id and parse it, store this in variable (maybe tracks)
  4. Create a variable, an empty string (maybe html)
  5. Iterate over tracks list, take a template for HTML5 widget* and replace key for sound id with sound id of current track (currentTrack.id) on each iteration, add the processed template into html)
  6. Print html out

*Notes:

  • Template for HTML5 widget as a string (replace {trackId} with current sound id while iterating, and {widgetParams} with something like &color=ff6600&auto_play=false of course matching the desired params for your own embed) (more on this can be found here):

    <iframe width="100%" height="166" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F{trackId}{widgetParams}"></iframe>
    
  • When I say process template, I mean that you need to replace “keys” with some other strings, you can do that easily in different languages, either replace method of String type, or google “variable interpolation”.

  • The base API URL can be different – you can use q=QUERY with different endpoints such as

    • /users/USER_ID/tracks.json
    • /groups/GROUP_ID/tracks.json


    Or you can combine filters like tags http://api.soundcloud.com/tracks.json?q=YOUR_QUERY&tags=sermon&client_id=YOUR_CLIENT_ID

For API responses and options you have refer to SoundCloud API Reference



来源:https://stackoverflow.com/questions/15398646/retrieve-soundcloud-search-results-and-embed-results

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