flash air MP3播放器as3代码
flash air MP3播放器as3代码,这是以前在粉色男孩百度空间里学到的一点AIR代码,自己发挥了一下捣腾了一个AIR播放器出来,在“资源下载”页面底部有生成后的播放器下载,可以播放MP3格式的本地歌曲。现在时间久了快忘光了,今天把代码发上来当是备忘吧!
var sound:Sound;//声明Sound类对象
var channel:SoundChannel=new SoundChannel ;//声明声音通道对象,用于控制声音播放
var pos:Number=0;//声明变量存储进度
var total:Number;//音乐总的长度
var w:Number=bar_mc.width;//进度条的原始宽度
bar_mc.width=0;//进度条宽度清0
var flag:Boolean=false;//播放标记
var mp3FileFilter:FileFilter=new FileFilter(“MP3文件”,”*.mp3;*.MP3;”);//文件过滤器对象
var window:NativeWindow=stage.nativeWindow;//本地窗口对象;对包含此舞台的 NativeWindow 对象的引用。窗口表示本机操作系统的窗口,舞台表示窗口所包含的内容。
////////////////////
var arr:Array=[];
var leng:int;
//========================================================
open_btn.addEventListener(MouseEvent.MOUSE_DOWN,openMp3);
function openMp3(e:MouseEvent):void {
pos=0;//打开新文件时清除上次的播放进度
var file:File=new File();
//File 对象表示文件或目录的路径。这可以是现有的文件或目录,也可以是尚不存在的文件或目录(例如,它可以表示希望创建的文件或目录的路径)。
file.addEventListener(Event.SELECT,selectMp3);//文件被选中时
file.browseForOpen(“打开MP3文件”,[mp3FileFilter]);//为打开而浏览
}
function selectMp3(e:Event):void {
var file:File=File(e.target);
////////////////////////////////////////
arr.push(file.url);
leng=arr.length-1;
file.removeEventListener(Event.SELECT,selectMp3);//可省掉
sound=new Sound();
/////////////////////////////////////////////
sound.load(new URLRequest(arr[leng]));
sound.addEventListener(Event.COMPLETE,mp3Loaded);
}
function mp3Loaded(e:Event):void {
sound.removeEventListener(Event.COMPLETE,mp3Loaded);//可省掉
total=sound.length;
channel.stop();
pos=0;
channel=sound.play(pos);
play_btn.visible=false;
pause_btn.visible=true;
addEventListener(Event.ENTER_FRAME,frame);
}
//=============================================
pause_btn.addEventListener(MouseEvent.MOUSE_DOWN,pauseMp3);
function pauseMp3(e:MouseEvent):void {
pos=channel.position;
channel.stop();
play_btn.visible=true;
pause_btn.visible=false;
_sprite.graphics.clear();
//removeEventListener(Event.ENTER_FRAME,frame);//与停止事件不同,pos不能归0,所以不可以设置flag为true,此处帧频不必移除
}
//============================
stop_btn.addEventListener(MouseEvent.MOUSE_DOWN,stopMp3);
function stopMp3(e:MouseEvent):void {
pos=0;
channel.stop();
play_btn.visible=true;
pause_btn.visible=false;
if (flag==false) {
flag=! flag;//与暂停不一样,移除帧频在flag为false时执行了
}
}
//================================
play_btn.addEventListener(MouseEvent.MOUSE_DOWN,playMp3);
function playMp3(e:MouseEvent):void {
//channel.stop();//如果play_btn按钮一直可见,此句必需
if (sound!=null) {
channel=sound.play(pos);
play_btn.visible=false;
pause_btn.visible=true;
if (flag==true) {
flag=! flag;
}
addEventListener(Event.ENTER_FRAME,frame);
} else {
trace(“未选歌曲”);
}
}
//===============================================
prev_btn.addEventListener(MouseEvent.CLICK,prevMp3);
function prevMp3(e) {
if (sound!=null) {
pos=0;
channel.stop();
sound=new Sound(new URLRequest(arr[leng=(--leng+arr.length)%arr.length]));
channel=sound.play(pos);
play_btn.visible=false;
pause_btn.visible=true;
addEventListener(Event.ENTER_FRAME,frame);
flag==true?flag=false:0;
} else {
trace(“无歌曲”);
}
}
next_btn.addEventListener(MouseEvent.CLICK,nextMp3);
function nextMp3(e) {
if (sound!=null) {
pos=0;
channel.stop();
sound=new Sound(new URLRequest(arr[++leng%arr.length]));
channel=sound.play(pos);
play_btn.visible=false;
pause_btn.visible=true;
addEventListener(Event.ENTER_FRAME,frame);
flag==true?flag=false:0;
} else {
trace(“也无歌”);
}
}
//=============================================
function frame(e:Event):void {
showWave();
pos=channel.position;
total=sound.length;
bar_mc.width=pos/total*w;
if (flag) {
bar_mc.width=0;
play_btn.visible=true;
pause_btn.visible=false;
pos=0;
_sprite.graphics.clear();
removeEventListener(Event.ENTER_FRAME,frame);
}
channel.addEventListener(Event.SOUND_COMPLETE,soundComplete);
}
function soundComplete(e) {
nextMp3(e);//播放完毕,自动调用下一曲函数
}
//================窗口关闭和移动==================
quit_btn.addEventListener(MouseEvent.MOUSE_DOWN,quit);
function quit(e:MouseEvent):void {
window.close();
}
stage.addEventListener(MouseEvent.MOUSE_DOWN,drag);
function drag(e:MouseEvent):void {
window.startMove();
}
//=======================下面是声波===========
var _sprite:Sprite=new Sprite();
addChild(_sprite);
var btArray:ByteArray = new ByteArray();
//ByteArray 类提供用于优化读取、写入和处理二进制数据的方法\属性
var colors:Array=[0xff0000,0x0000ff];
function showWave() {
var k:Number;
_sprite.graphics.clear();
SoundMixer.computeSpectrum(btArray,true,0);
//声音混频器类的计算成本频谱方法,其结果放入第一个参数字节数组中
//第二个为false则波形上下双向浮动,将此参数设置为 true 会导致方法返回的是频谱而不是原始声音波形。 在频谱中,左侧表示的是低频,右侧表示的是高频。 第三个0是声音采样率是44.1KHz
for (var i:int=0; i < 512; i=i+12) {
k=btArray.readFloat();
//从字节流或字节数组中读取 IEEE 754 单精度浮点数。
var h:Number=k*11;
var colorIndex:uint=Math.random()*colors.length;
_sprite.graphics.lineStyle(2,colors[colorIndex],0.4);
_sprite.graphics.moveTo(i/4+9,47);//除以是指横向排列的更密一点
_sprite.graphics.lineTo(i/4+9,47-h);
}
}
//=======================下面是播放进度选择===========
stage.addEventListener(MouseEvent.CLICK,jinDu);
function jinDu(e:MouseEvent):void {
if (mouseY>45&&mouseY<55) {
if (sound!=null) {
var juLiBi:Number=stage.mouseX/200;
pos=juLiBi*total;
channel.stop();
channel=sound.play(pos);
addEventListener(Event.ENTER_FRAME,frame);
play_btn.visible=false;
pause_btn.visible=true;
if (flag==true) {
flag=! flag;
}
pos=0;
} else {
trace(“无歌!”);
}
}
}
//========================托盘图标======
var ic:Ic=new Ic();
var bmd:BitmapData=new BitmapData(16,16,true,0×99ffffff);
bmd.draw(ic);
var bit:Bitmap=new Bitmap(bmd);
NativeApplication.nativeApplication.icon.bitmaps=[bit];
SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK,clickIcon);
SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.RIGHT_CLICK,rightClickIcon);
function clickIcon(e) {
window.visible=! window.visible;
}
function rightClickIcon(e) {
window.close();
}
1条评论 ▼