问题
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:
- Get search query from user
- Issue a request to http://api.soundcloud.com/tracks.json?q=YOUR_QUERY&client_id=YOUR_CLIENT_ID*
- 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
) - Create a variable, an empty string (maybe
html
) - Iterate over
tracks
list, take a template for HTML5 widget* and replace key for soundid
with sound id of current track (currentTrack.id
) on each iteration, add the processed template intohtml
) - 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 tagshttp://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