How to get proper soundcloud url for embedding from a standard url?

qitch picture qitch · Mar 11, 2012 · Viewed 22.1k times · Source

So right now I can grab the url from already embedded soundcloud files in embed or iframe tags and use those urls on another page and display it in fancybox. Is there a way to get the right url or display in fancybox if a user offers a typical share url such as http://soundcloud.com/song-b/song-please or http://snd.sc/yp6VMo? I tried looking at the api documentation but I couldn't really find what I was looking for.

Answer

Lincoln B picture Lincoln B · Mar 12, 2012

He is trying to get html to create a Soundcloud widget, not get the API url for a track. The oEmbed method "will serve the widget embed code for any SoundCloud URL pointing to a user, group, set or a playlist" and is recommended to make sure Soundclouds changes don't break your app (as opposed making your own Flash object or iframe). You can call it with a PHP web request to

http://soundcloud.com/oembed?format=js&url=[escaped_url]&iframe=true

Or with a JSON-P call (using jQuery for convenience)

$.getJSON('http://soundcloud.com/oembed?callback=?',
    {format: 'js', url: 'http://snd.sc/yp6VMo', iframe: true},
    function(data) {
        // Stick the html content returned in the object into the page
        $('#your_player_div').html(data['html'])
    }
)

I'm adding iframe=true so you get the new HTML5 player, omit to get the old Flash object. It also works with the .sc short urls you are using.