1

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!

João Pereira
  • 3,545
  • 7
  • 44
  • 53
  • How do you send JSON to player without using ExternalInterface? – Nek Aug 05 '11 at 10:53
  • Sorry, my bad; i thought we didn't, but I just spoke with the player's dev and he said we do use it; just updated the title. – João Pereira Aug 05 '11 at 10:55
  • Are you absolutely sure the json_str contains proper data at the time you pass it to player? – Nek Aug 05 '11 at 11:12
  • I think so, the console.log is being outputted before that command is sent to the player, so I don't see any reason not to. This is also the first time this kind of problem has appeared, we've been sending many other commands for quite some time and everything works fine. – João Pereira Aug 05 '11 at 11:16
  • 1
    Looks very close to your problem http://stackoverflow.com/questions/5149387/why-does-externalinterface-breaks-when-i-pass-parameter-with-json-like-string – Nek Aug 05 '11 at 16:34

2 Answers2

3

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.

Lars Blåsjö
  • 6,118
  • 2
  • 19
  • 23
2

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

The_asMan
  • 6,364
  • 4
  • 23
  • 34