Search code examples
soundcloudsoundmanager2

Soundmanager2 Soundcloud API crossdomain.xml


I am using Soundmanager2 to stream files from soundcloud and display Eq visuals, but I'm having trouble with getting the eqdata when pausing or changing a track.

Shown here

I understand that Flash is unable to access the metadata due to the cross domain policy defined in the "crossdomain.xml" file on soundcloud as seen in this post: (and many others)

How to use SoundManager2 to stream from SoundCloud, and make visualizations?

I realize that I have to resolve the track's stream_url before loading it into sound manager. I'm doing this with an ajax call to a php script that resolves the url (shown below):

var client_id = '866143113772fec9556700f7f88f3abc',
url = 'http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/aries-audio-music/tracks&client_id=';

$.getJSON(url+client_id+'&callback=?', function(playlist){

    $.each(playlist, function(index, track) {

        //append to playlist
        $('<li id="tr-'+track.id+'">' + track.title + '</li>').data('track', track).appendTo('.tracks');

        //get resolved stream url
        $.ajax({
            type: 'GET',
            url: 'get_sc_url.php?id='+track.id, 
            success: function(data) { 
                console.log(data);
                sm2_addtrack(track, data);      //create SM2 sound object with resolved url
            }
        });
    });
});

function sm2_addtrack(track, stream_url) {
    soundManager.createSound({
        id: 'track_' + track.id,
        url: stream_url,            
        usePolicyFile : true,
        usePeakData: false,
        useWaveformData: false,
        useEQData: true,
.....

get_sc_url.php used to resolve stream_url

<?php

require 'include/referrer_check.php';
require 'include/SC_API_KEY.php';
require 'include/API_cache.php';

$track_id = intval($_GET['id']);
$key = get_soundcloud_api_key();
$api_call = 'http://api.soundcloud.com/tracks/'.$track_id.'/stream/?client_id='.$key;

function get_web_page($url) {

    /*
     * hat tip: http://forums.devshed.com/php-development-5/curl-get-final-url-after-inital-url-redirects-544144.html
    */

    $options = array( 
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RETURNTRANSFER => false,     // return web page 
        CURLOPT_HEADER => true,
        CURLOPT_NOBODY => true,
        CURLOPT_CONNECTTIMEOUT => 5,        // timeout on connect 
        CURLOPT_TIMEOUT        => 5,        // timeout on response 
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_RETURNTRANSFER => true,     // return web page
    ); 

    $ch      = curl_init( $url ); 
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    return $header;
}  

$myUrlInfo = get_web_page($api_call);

echo $myUrlInfo["url"];

?>

The result I am getting is something like this:

ec-media.soundcloud.com/Ez0B3lUZjjCR.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a52200996dfabd3cefb29b7554ff4fd02baab5100d3a070e07d55f6e1eb41808c65398ce84cd496788c171f7e4&AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1415223069&Signature=A0qaC1Nr3%2FXw4jwFYMjA%2F98arwI%3D

which plays but gives me no spectrum data at all and I still get the computeSpectrum() (EQ data) SecurityError: Error #2123 from soundmanager2.

I know for sure that ec-media.soundcloud.com/crossdomain.xml is being downloaded, but I still can't solve this problem.


Solution

  • I think it may be because I'm trying to access the soundcloud crossdomain.xml policy file (which is HTTPS) from a HTTP document. secure="false" is not defined in Soundcloud's crossdomain.xml policy file so it defaults to secure="true", therefore the SWF can not access anything.

    I doubt Soundcloud will ever set secure="false" in their crossdomain.xml for obvious reasons (it defeats the purpose of even having HTTPS).

    I "think" this is why I get computeSpectrum() (EQ data) SecurityError: Error #2123, but I don't have access to a HTTPS enabled server to test this so I could be wrong.