我的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));
以及这个"主题"的其他变体,但似乎什么都不起作用。我只是在这里或那里的语法上挣扎吗?谢谢你的帮助。
你似乎就快到了。这是一个修订版。如果有效,请告诉我。
function playSound(mp3file::String) {
trace(mp3file);
}
ExternalInterface.addCallback("playSound", playSound);
另请参阅:http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cb2.html
事实证明,一开始是正确的。错误在其他地方,我忘了嵌入一个字体:/
然而,对于那些构建类似东西的人来说,这里有完整的代码。我也不确定我停止轨道的方法是否正确,所以任何改进都是最受欢迎的:)
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 ?>