flash9制作播放音乐时使频谱真实显示的代码

  • 来源: 52design.com   2016-04-28/10:14
  • (需要首先将mp3转换为swf,再对转换后的swf进行处理,才能正确显示声音频谱),可以说Flash8及以下的版本对声音的支持是有限的,而Flash9在此方面进行了升级,对声音的处理能力大为提升,下面发一个用flash9制作的播放音乐时频谱真实显示的小实例(参考了他人心得),界面如下图1:

    1.打开Flash9,在主场景第一帖上(仅此一帧):

    import flash.display.Sprite;   

    import flash.display.BlendMode;  //混合模式类  

    import flash.events.*;   //事件类 

    import flash.media.Sound;    //声音类 

    import flash.media.SoundMixer;  //混音器类  

    import flash.media.SoundChannel; //声道类   

    import flash.net.URLRequest;    //URLRequest类

    import flash.utils.ByteArray;   //ByteArray类 

    import flash.display.Bitmap; //位图类

    import flash.display.BitmapData; 

    import flash.filters.BlurFilter; //滤镜类

    import flash.filters.ColorMatrixFilter;  

    import flash.filters.BitmapFilterQuality;  //滤镜品质类 

    import flash.geom.Rectangle;    //矩形类

    import flash.geom.Point;  //Point类(点)

    Stage.showDefaultContextMenu = false;

    //声明用来包含line和bg的Sprite

    var Main:Sprite=new Sprite();

    //声明用来画线的Sprite

    var line:Sprite=new Sprite();

    //声明用来放位图数据的BitmapData

    var bmpData:BitmapData=new BitmapData(350,200,true,0xFF0000);

    //声明用来显示效果的Bitmap

    var bmp:Bitmap=new Bitmap(bmpData);

    //声明一个ByteArray对象

    var bArray:ByteArray = new ByteArray();

    //声明一个数组对象

    var Ary:Array;

    //声明两个数字对象

    var n:Number = 0;

    var c:Number = 0;

    //声明一个ColorMatrix滤镜

    var colorM:ColorMatrixFilter=new ColorMatrixFilter([0.98,0,0,0,0,0,0.98,0,0,0,0,0,0.98,0,0,0,0,0,0.90,0,]);

    //声明一个BlurFilter滤镜

    var blur:BlurFilter = new BlurFilter(7,7,BitmapFilterQuality.LOW);

    //声明一个矩形

    var r:Rectangle=new Rectangle(0,0,350,200);

    //声明一个点

    var point=new Point(0,0);       

    function showBar1(event:Event):void{

    n = 0;

    //这里是为了每2次才执行一次滤镜而做的if,如果需要让原来的波形图消失的更慢就把2改成更大的数字

    if(c%2==0){

    //将Main的内容绘制到bmpData

    bmpData.draw(Main);

    //应用滤镜

    bmpData.applyFilter(bmpData,r,p,colorM);

    bmpData.applyFilter(bmpData,r,p,blur);

    }

    c++;

    //清除绘图

    line.graphics.clear();

    //设置线条样式,颜色green,宽度1,透明度100

    line.graphics.lineStyle(1,0x00CC00,100);

    //将当前声音输出为ByteArray,注意哦,这次用的是false,上次是true

    SoundMixer.computeSpectrum(bArray,false,0);

    for(var i=0; i<256; i+=2){

    //在ByteArray中读取一个32位的单精度浮点数

    n = bArray.readFloat();

    //这个实际作用是把n扩大一下

    n = n*360;

    //如果i不为0

    if(i!=0){

    //画波形图

    line.graphics.lineTo(50+i,100-n/5);

    }else{

    //移动    

    line.graphics.moveTo(50,100-n/5);

    }

    }

    }

    //

    play_btn.addEventListener(MouseEvent.MOUSE_DOWN,playSounds);

    stop_btn.addEventListener(MouseEvent.MOUSE_DOWN,stopSounds);

    pause_btn.addEventListener(MouseEvent.MOUSE_DOWN,pauseSounds);

    var kaiguan:Boolean =false;

    function playSounds(event:MouseEvent):void {

    if(kaiguan){

    _channel = _sound.play(_position);

    kaiguan=false;

    }

    }

    function stopSounds(event:MouseEvent):void {

    //....设置为0位置才能使进度条归零.....

    _channel = _sound.play(0);

    //......停止声音的方法........

    SoundMixer.stopAll( ); 

    kaiguan=true;

    }

    function pauseSounds(event:MouseEvent):void {

    if(!kaiguan) {

    _position = _channel.position;

    _channel.stop( );

    kaiguan = !kaiguan;

    }           

    }

    //

    var _position:int =0;

    this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

    function onEnterFrame(event:Event):void{ 

    //..........进度条初始值.........................

    var barWidth:int = 271;

    var barHeight:int = 5; 

    var loaded:int = _sound.bytesLoaded;

    var total:int = _sound.bytesTotal;            

    var length:int = _sound.length;

    _position = _channel.position;

    //................ 画进度条背景....................................

    graphics.clear(  );

    graphics.beginFill(0x333333);

    graphics.drawRect(40, 187, barWidth, barHeight);

    graphics.endFill(  );

    //..................................................................

    //.............时间显示设置区................................................

    var miao:int=Math.round(length/1000)-Math.round(length/60000)*60;

    var fen:int=Math.round(length/60000);

    if(fen<10){

    if(Math.abs(miao)<10){

    musicTimer.text="0"+fen+":0"+ Math.abs(miao);

    }else{

    musicTimer.text="0"+fen+":"+ Math.abs(miao);}

    }else{

    if(Math.abs(miao)<10){

    musicTimer.text=fen+":0"+ Math.abs(miao);

    }else{

    musicTimer.text=fen+":"+ Math.abs(miao);

    }

    }

    var playmiao:int=_position/1000;

    if(playmiao<60){

    if(playmiao<10){

    musicPlayingTimer.text="00:0"+playmiao;

    }else{

    musicPlayingTimer.text="00:"+playmiao;

    }

    &


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多