JavaScript / jQuery Flash AS3 interaction
JavaScript / jQuery Flash AS3 interaction
我正在设计一个音乐播放器使用JavaScript (jQuery)和HTML5,与Flash AS3回落。基本上我想做的是能够点击HTML控制元素,并让他们与flash进行交互,以便播放/暂停和跳过播放列表中的曲目(播放列表JSON文件由JavaScript读取,传递文件ID给AS3, AS3读取另一个JSON文件获取URL,然后播放音频)
这使我能够只使用Flash播放音频,从而创建相同的用户体验,而不管HTML5浏览器支持。
我假设我将不得不"监听"AS3中的事件,但是任何关于如何在JS中参与这些事件并对AS3中的事件做出反应的指针将是一个很大的帮助!
要在JavaScript和ActionScript之间进行通信,可以使用ExternalInterface API:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html作为你想做的另一种选择,你可以使用SoundManager 2来播放音频,并在JavaScript中做所有你自己的编程:
http://www.schillmania.com/projects/soundmanager2/"使用HTML5和Flash, SoundManager 2提供可靠的跨平台音频在一个JavaScript API下。"
我对这个问题有一个答案,但是在我完成之前这个答案就被接受了。不管怎样,它是:
主要。(文档类):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _sound:Sound;
private var _soundChannel:SoundChannel;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
if (ExternalInterface.available)
{
ExternalInterface.addCallback("loadSound", loadSound);
ExternalInterface.addCallback("stopSounds", stopSounds);
}// end if
}// end function
private function loadSound(url:String):void
{
_sound = new Sound();
_sound.load(new URLRequest(url));
if (_soundChannel) _soundChannel.stop();
_soundChannel = _sound.play();
}// end function
private function stopSounds():void
{
_soundChannel.stop();
}// end function
}// end class
}// end package
sounds.json:
{ "sounds" : {
"sound": [
{ "name": "Sound 1", "url": "sounds/sound1.mp3" },
{ "name": "Sound 2", "url": "sounds/sound2.mp3" },
{ "name": "Sound 3", "url": "sounds/sound3.mp3" }
]
}}
index . html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>SoundPlayer</title>
<meta name="description" content="" />
<script src="js/swfobject.js"></script>
<script src="js/jquery.min.js"></script>
<script>
var flashvars = {
};
var params = {
menu: "false",
scale: "noScale",
allowFullscreen: "true",
allowScriptAccess: "always",
bgcolor: "",
wmode: "direct" // can cause issues with FP settings & webcam
};
var attributes = {
id:"SoundPlayer"
};
swfobject.embedSWF(
"SoundPlayer.swf",
"altContent", "0", "0", "10.0.0",
"expressInstall.swf",
flashvars, params, attributes);
</script>
<style>
html, body { height:100%; overflow:hidden; }
body { margin:0; margin-top:25px; }
.button { float:left; margin-left:25px; width:100px; height:60px;
background-color:#fafafa; border: 1px solid #e1e1e1;
font-size:15px; font-family: Arial; text-align:center; padding-top:40px;
text-decoration: none; color: #323232; }
</style>
</head>
<body>
<script>
$(document).ready(function() {
var soundPlayer = $("#SoundPlayer").get(0);
$.getJSON('json/sounds.json', function(data) {
$.each(data.sounds.sound, function(i, sound) {
$("<a href='"#'" class='"button'">" + sound.name + "</a>")
.click(function () { soundPlayer.loadSound(sound.url); })
.appendTo("body");
});
$("<a href='"#'" class='"button'">Stop Sounds</a>")
.click(function() { soundPlayer.stopSounds(); })
.appendTo("body");
});
});
</script>
<div id="altContent">
<h1>SoundPlayer</h1>
<p><a href="http://www.adobe.com/go/getflashplayer">Get Adobe Flash player</a></p>
</div>
</body>
</html>
相关文章:
- Flash AS3关闭浏览器窗口
- 如何将一个*参数*从JS传递到AS3
- Javascript:如何对属性变量(例如var.hello.push)执行数组方法?太习惯AS3了
- as3中enter_frame的javascript版本
- 如何使用外部接口将参数从 JavaScript 传递到 AS3
- 在 AS3 和 HTML5 中创建 3D 条形图
- Javascript 在尝试将 var 发送到 AS3 ExternalInterface 时抛出错误
- 确保浏览器 AS3 支持限制
- AS3 XML上的翻页在IE和FIREFOX上看不到
- 为什么AS3 navigationToURL()只运行一次
- Facebook AS3 SDK without JavaScript
- 如何从AdobeAIR应用程序内的网页调用AS3代码
- Javascript未将数据返回到AS3闪存
- Facebook Flash AS3 应用程序 - swc 不起作用
- 在Google Chrome Extension中调用Javascript中的Flash As3函数
- 如何在 AIR Android 应用程序中在 JS 和 AS3 之间进行通信
- AS3 thinks e.target.bytesTotal = 0
- 将 Base64 字符串从 AS3 返回到 Javascript.这是一个错误或功能(还是我错了?
- 将 AS3 转换为 JavaScript
- JavaScript / jQuery Flash AS3 interaction