简体   繁体   English

JSON Javascript到Flash的通信问题(使用ExternalInterface):硬编码的字符串有效,否则不会

[英]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. 在尝试向我公司的Flash Player发送JSON命令时遇到了一个奇怪的问题。 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. 但是最奇怪的部分是,如果我使用Firebug的console.log打印整个命令,然后将其复制并粘贴到代码中(从而对播放列表进行硬编码),那么一切都将像一个超级按钮。

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. 我已经尝试了所有可以想到的toString()技巧(例如:json_str.toString(),“”“ + json_str.toString()+'”等,等等),但到目前为止还没有这种运气。

Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

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. 如果找不到使用JSON字符串的解决方案,则可以尝试发送一个对象-一个JavaScript对象,而不是该对象的JSON表示形式-因为ExternalInterface会为您处理序列化。

In other words, objects can be sent between JavaScript and ActionScript directly using ExternalInterface, without doing any serialization and deserialization of your own. 换句话说,可以直接使用ExternalInterface在JavaScript和ActionScript之间发送对象,而无需自己进行任何序列化和反序列化。

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 是一个字符串,使用“\\”转义引号AS3已经知道与问题
Just pass the JavaScript object straight to the SWf. 只需将JavaScript对象直接传递给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 此代码未经测试,但应该可以给您一个提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM