BKWLD

 

CDN and computeSpectrum function do not get along

By Max on February 26, 2008 at 11:10 am

For the soundwave360 site we decided to use Limelight to stream the audio. Once the audio was moved over to the Limelight CDN everything was streaming a lot faster. However there was one problem, flash was throwing a sandbox security error every time I used the computeSpectrum function to create the sound wave.

So here’s the deally. Any time you grab data from a directory that is not a sub directory of your swf you need a crossdomain.xml policy file in the root directory of the url that you are grabbing the data from. This is a very common thing and well documented in flash. But we already had a crossdomain.xml file in our limelight root directory, or so we thought. The URL of our limelight CDN is appended with some weird ID number. So the directory that stores our streaming files, and our crossdomain.xml file, is actually a subdirectory of the root.

Flash automatically looks at the root directory for the crossdomain.xml file before loading any external content. But what if your policy file exists in a subdirectory like ours? Use the Security.loadPolicyFile function. Just specify this first thing before writing any other code, maybe in your document class. Flash will wait for any crossdomain policy files to load before loading any external data, so no event handlers are needed, not that there are any for this function.

Here’s what it should look like in your document class(AS3):

package com.mysite {

import flash.display.MovieClip;
import flash.system.Security;

public class MyDocumentClass extends MovieClip {
public function MyDocumentClass():void {
Security.loadPolicyFile("http://www.mysite.com/somefolder/crossdomain.xml");
}

}

}

The computeSpectrum function needs access to the data of the sound that you are loading. Because of this, a simple crossdomain.xml file will not give you permission to use this function. Rather it should look like this:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <allow-access-from domain="*" secure="false" to-ports="*">
</cross-domain-policy>

Without this file you will still be able to load, play and stop the sound, but you wont be able to access any of its data like the id3 information or the byte information, which computeSpectrum needs.

2 Comments »

  1. Hey man, I too have encountered this problem, i solved it just as you did but now I am facing a new one.

    From the next Flash Player version and so on, flash will always try to read the meta-policy file (which has to be at the root of the website, i.e “www.mysite.com/crossdomain.xml”), and only after reading that will it read the other policy files you’re asking it to (www.mysite.com/myfolder/crossdomain.xml.

    Only if the meta-policy file allows other policy files to be created, they will be taken into account.

    Since limelight doesn’t hold a meta-policy file in the root folder of its customers accounts, we’ll have a serious problems when the next version of the Flash Player will come out. And it’s due for next week.

    Have you thought of any solution because i haven’t? (well, except mailing limelight and asking them)

    Comment by Eldad — April 3, 2008 @ 2:24 am

  2. We did call limelight and ask them if it was possible to get a crossdomain.xml file in the root directory, and as we expected, no dice. I imagine that they will have to eventually address this problem given that everyone and their mom are using flash to stream video and audio. I guess time will tell.

    Comment by Max — April 10, 2008 @ 9:02 am


RSS feed for comments on this post. | TrackBack URI

Leave a comment