nsmyself
nsmyself

Reputation: 3565

JSON Javascript to Flash communication issue (with ExternalInterface): Hardcoded string works, otherwise it won't

I've ran into a weird problem while trying to send a JSON command to my company's flash player. Basically, i am unable to pass a playlist to this player - nothing happens - using the following command:

player.sendEvent("LOAD_PLAYLIST", json_str);

but the weirdest part is that if I print the entire command using Firebug's console.log, copy it and paste it into the code (thus hardcoding the playlist), everything works like a charm.

For instance, the following code:

player.sendEvent("LOAD_PLAYLIST", "{\"streams\": [{\"src\": \"rtmp:\/\/xxx.xxx.xx:80\/redirectvodxxx\/nas2.share\/h264\/512x384\/progname1\/prog_1_20110804.mp4\"},{\"src\": \"rtmp:\/\/xxx.xxx.xx:80\/redirectvodxxx\/nas2.share\/h264\/512x384\/progname2\/prog_2_20110804.mp4\"},{\"src\": \"rtmp:\/\/xxx.xxx.xx:80\/redirectvodxxx\/nas2.share\/h264\/512x384\/progname3\/prog_3_20110804.mp4\"}]}");

was obtained using

console.log('[loadNewListofContents] playing the following content list: player.sendEvent(\"LOAD_PLAYLIST\", ' + json_str.toString() + ');');

and if i hardcode it, it works! I've tried all the toString() tricks I can think of (ex: json_str.toString(), '"' + json_str.toString() + '"', etc...) but so far no such luck.

Any ideas? Thanks in advance!

Upvotes: 1

Views: 806

Answers (2)

The_asMan
The_asMan

Reputation: 6403

the code you posted

player.sendEvent("LOAD_PLAYLIST", "{\"streams\": [{\"src\": \"rtmp:\/\/xxx.xxx.xx:80\/redirectvodxxx\/nas2.share\/h264\/512x384\/progname1\/prog_1_20110804.mp4\"},{\"src\": \"rtmp:\/\/xxx.xxx.xx:80\/redirectvodxxx\/nas2.share\/h264\/512x384\/progname2\/prog_2_20110804.mp4\"},{\"src\": \"rtmp:\/\/xxx.xxx.xx:80\/redirectvodxxx\/nas2.share\/h264\/512x384\/progname3\/prog_3_20110804.mp4\"}]}");

Is a string that is using "\" to escape quotes AS3 has known issues with that
Just pass the JavaScript object straight to the SWf.

// And in your AS3 code add this
if(ExternalInterface.available){
  ExternalInterface.addCallBack("AS3functiontocall", AS3functiontocall );
}

function AS3functiontocall( var obj:Object ):void{
     trace( obj.streams[0] ); // might have to eval or JSON.decode the obj
}


// JavaScriptcode should look something like 
function sendList( ){
  var container;
  if (navigator.appName.indexOf("Microsoft") >= 0){
    container = document;
  }else{
    container = window;
  }
  var obj = {
      "streams": [
                   {"src": "rtmp://xxx.xxx.xx:80/redirectvodxxx/nas2.share/h264/512x384/progname1/prog_1_20110804.mp4"},
                   {"src": "rtmp://xxx.xxx.xx:80/redirectvodxxx/nas2.share/h264/512x384/progname2/prog_2_20110804.mp4"},
                   {"src": "rtmp://xxx.xxx.xx:80/redirectvodxxx/nas2.share/h264/512x384/progname3/prog_3_20110804.mp4"}
                 ]
  }

  var result = container["yourswfnamehere"].AS3functiontocall ( obj );
}

This code is untested but it should give you an idea

Upvotes: 2

Lars Blåsjö
Lars Blåsjö

Reputation: 6127

If you don't find a solution using JSON strings, maybe you could instead try sending an object - a JavaScript object, rather than a JSON representation of the object - since ExternalInterface takes care of serialization for you.

In other words, objects can be sent between JavaScript and ActionScript directly using ExternalInterface, without doing any serialization and deserialization of your own.

Upvotes: 3

Related Questions