javascript 与flash 通信互传参数,以flash播放器为例

本文介绍了一种使用Flash与JavaScript进行交互的方法。首先在Flash中定义了与外部交互的函数,然后通过HTML页面上的按钮触发这些函数来实现加载MP3和控制播放暂停的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人在iteye第一个原创贴

第一步、新建一个.fla 文件在第一帧的动作内加入如下代码

import flash.external.ExternalInterface;
var played=0;
var sta=false;
s=new Sound();
function addmp3(url){
s.loadSound(url,true);
}
function stopPause(){
if(sta){
s.start(played/1000,1);
sta=false;
}else{
sta=true;
played=s.position;
s.stop();
}

}


ExternalInterface.addCallback("stopPause",null,stopPause); //将flash内函数提供给外部javascript使用
ExternalInterface.addCallback("addmp3",null,addmp3);


第二步、生成flash文件,在网站根目录下新建一个mp3.html 文件加入如下代码


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript flash</title>
</head>
<body bgcolor="#ffffff" >


<button onclick="a_dd()">加载</button>


<button onclick="s_p()">暂停/播放</button>


<script>
function addswf(){
var jj=document.createElement(&#39object');
jj.data=&#39abcff.swf'
jj.width=300;
jj.height=120;
jj.id="myContent";
jj.type="application/x-shockwave-flash";
document.body.appendChild(jj);
}


addswf();


var s=document.getElementById(&#39myContent');
function a_dd(){
s.addmp3(&#39music/ccc.mp3');
}
function s_p(){
s.stopPause();
}


</script>
</body>
</html>


第三步、把ccc.mp3文件放在根目录下的music文件夹下,运行mp3.html文件即可


注意:
addswf()函数是将flash通过js加载到页面上。如果直接加载flash在chrome下会出现下面的错误提示:Object #<HTMLObjectElement> has no method
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值