【问题标题】:How do I pass a *parameter* from JS to AS3?如何将 *parameter* 从 JS 传递到 AS3?
【发布时间】:2012-09-02 12:03:02
【问题描述】:

我的 swf 文件中有一个函数:playSound(),我可以像这样通过 jQuery 调用它:

$.talkToFlash("interfaceSounds").playSound();

而 'talkToFlash' 只返回 swf 文件。这部分在这里有很好的记录并且工作正常。

但是,我该如何传递参数呢?

我相信 JS 会是这样的:

$.talkToFlash("interfaceSounds").playSound('swish.mp3');

但是在 AS3 中我该怎么做呢?

我尝试修改我的初始工作功能:

function playSound() {
    var s:swishSound = new swishSound; 
    var channel:SoundChannel = s.play();
}
ExternalInterface.addCallback("playSound", playSound);

用这个:

function playSound(mp3file) {
   trace(mp3file);
}

但“mp3file”只是空的。我试过了

ExternalInterface.addCallback("playSound", playSound(mp3file));

和这个“主题”的其他变体,但似乎没有任何效果。我只是在这里和那里的语法上挣扎吗?感谢您的帮助。

【问题讨论】:

    标签: javascript jquery flash parameter-passing


    【解决方案1】:

    你似乎快到了。这是一个修订版。让我知道它是否有效。

    function playSound(mp3file::String) {
      trace(mp3file);
    }
    
    ExternalInterface.addCallback("playSound", playSound);
    

    另见:http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cb2.html

    【讨论】:

      【解决方案2】:

      事实证明一开始是正确的。错误在其他地方,我忘记嵌入字体:/

      不过,对于那些构建类似东西的人来说,这里是完整的代码。我也不确定我停止轨道的方法是否正确,所以欢迎任何改进:)

      AS3:

      import flash.external.ExternalInterface;
      import flash.media.Sound;
      import flash.media.SoundLoaderContext;
      import flash.net.URLRequest;
      
      function playTrack(track_name) {
      
          var s:Sound = new Sound();
          soundName.text =track_name;
          track_name = '/path/to/mp3/' + track_name + ".mp3";
          var req:URLRequest = new URLRequest(track_name);
          var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
          s.load(req, context);
          s.play();
      
      }
      
      function stopAllTracks() {
      SoundMixer.stopAll();
      }
      
      ExternalInterface.addCallback("playTrack", playTrack);
      ExternalInterface.addCallback("stopAllTracks", stopAllTracks);
      

      在 jQuery 中:

      // play a song
        $.curPlay = '';
        $(".song_play").click(function() {    
          var id = $(this).attr('id');
          song = id.replace('play_', '');
          if(song==$.curPlay){  // stop the current song if the play btn is clicked twice
              $.stop_mp3();
              $.curPlay = '';
              return;
          }
      
          $.curPlay = song;
          $.play_mp3(song);
      });
      
      
      
      $.play_mp3 = function(file){ 
          $.stop_mp3();
          $.talkToFlash("interfaceSounds").playTrack(file);   
      }
      
      $.stop_mp3 = function(){  
          $.talkToFlash("interfaceSounds").stopAllTracks();   
      }
      
      
      
      
      //  movie referencer 
      $.talkToFlash = function(swfFile){
      
        if (window.document[swfFile])   {
            return window.document[swfFile];
        }
        if (navigator.appName.indexOf("Microsoft Internet")==-1){
          if (document.embeds && document.embeds[swfFile])
            return document.embeds[swfFile]; 
        }
        else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
        {
          return document.getElementById(swfFile);
        }
      }
      

      还有 HTML/PHP:

          <div class='song_play'  id='play_<?=$songID?>'>play</div>
      

      我还有一个可能值得分享的技术,因为我发现在使用 Flash 进行开发时它非常有用。如果您在 PHP 中设置一些配置 Flash 的变量,则可以轻松更改容器的外观或爆破缓存:

      <? 
      $swf = "mp3_player.swf?".time();
      $w = 1;
      $h = 1;
      
      $mp3Player = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8\" 
       codebase = \"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" 
       width=\"$w\" height=\"$h\" id=\"interfaceSounds\" align=\"middle\">
       <param name=\"allowScriptAccess\" value=\"always\" />
       <param name=\"allowFullScreen\" value=\"false\" />
       <param name=\"movie\"  value=\"/assets/swf/$swf\" />
        <param name=\"quality\" value=\"high\" />
       <embed src=\"/assets/swf/$swf\" quality=\"high\" width=\"$w\" 
       height=\"$h\" name=\"interfaceSounds\" align=\"middle\" 
       allowScriptAccess=\"always\"  allowFullScreen=\"false\"
        type=\"application/x-shockwave-flash\"    
       pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
       </object>";
      

      稍后在您的 php 文档中,您只需在任何您想要的地方回显它:

        <?= $mp3Player ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-03
        • 1970-01-01
        • 2017-10-10
        • 2019-07-11
        • 2021-04-19
        • 1970-01-01
        • 2018-06-25
        相关资源
        最近更新 更多