How to dynamically embed the “tiny” version of the Soundcloud player using the JS SDK?

醉酒当歌 提交于 2019-12-06 07:10:59
Misha Reyzlin

Unfortunately, /oembed only works for regular widget (HTML5 of flash) at the moment.

However, you can build the html for either of them yourself. Here's a template for tiny widget embed:

<object height="18" width="100%">
  <param name="movie" value="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F{trackId}&player_type=tiny{widgetParams}"></param>
  <param name="allowscriptaccess" value="always"></param>
  <param name="wmode" value="transparent"></param>
  <embed wmode="transparent" allowscriptaccess="always" height="18" width="100%" src="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F{trackId}&player_type=tiny{widgetParams}"></embed>
</object>

And the template for regular widget:

<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>

You should put these into strings (without line breaks) and perform variable interpolation on these strings. I am not sure what language you are using, but most languages have this built in.

For JavaScript, which I'm writing most of the time, you could use Douglas Crockford’s supplant function.

The variables are trackId for which the value should be positive integer and widgetParams, for which the value should be something like &color=ff6600&auto_play=false, that is key=value pairs prefixed with &. I've used { and } as delimiters for variables to interpolate in my example, but it could, of course, be whatever your language or templates engine requires it to be.

For your client to be able to just give you a track URL, you'd need to do an API request to /resolve endpoint to retrieve track ID from track URL (permalink). You'd need to register as a developer to have client_id to authorise requests to the API. I've described how to do it in JavaScript in another answer here.

If you'd prefere not to do requests to the API, you could also ask your client to give you the embed code they can easily retrieve from any sound or set on SoundCloud by hitting "share" button, and then extract the sound or set ID using regular expression.

Here's an example of how this would work to insert a tiny widget (also using SoundCloud JS SDK) http://jsbin.com/OLUloX/1/edit

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