【FLEX】实现音频播放的波浪效果【转】

 flex音频播放的波形效果

这是封装好的一个:
package com
{
    import flash.display.*;
    import flash.events.Event;
    import flash.media.*;
    import flash.utils.*;
    import flash.filters.*;
    import flash.geom.*;
    import mx.core.UIComponent;
 
    public class Visualization extends UIComponent
    {
        public function Visualization()
        {
            //TODO: implement function
            super();
        }
 
 
        //private const plot_height:int = 50;
        private const CHANNEL_LEN:int = 256;
 
 
        public function set Audioswitch(b:Boolean):void
        {
            if(b)
            {
                this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
            else
            {
                this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
        }
 
 
 
        private function onEnterFrame(event:Event):void
        {
            var len_step:Number = this.width/CHANNEL_LEN;
            var plot_height:Number = this.height/2;
 
            var bytes:ByteArray = new ByteArray();
            SoundMixer.computeSpectrum(bytes, false, 0);
 
            var g:Graphics = this.graphics;
 
            g.clear();
            g.lineStyle(0, 0xe0d1f0);
            //g.beginFill(0x6600CC);
            g.moveTo(0, plot_height);
 
            var n:Number = 0;
 
            // left channel
            for (var i:int = 0; i < CHANNEL_LEN; i++)
            {
                n = (bytes.readFloat() * plot_height);
                g.lineTo(i * len_step, plot_height - n);
            }
            g.lineTo(CHANNEL_LEN * len_step, plot_height);
            //g.endFill();
 
            // right channel
            g.lineStyle(0, 0xCC0066);
            //g.beginFill(0xCC0066, 0.5);
            g.moveTo(CHANNEL_LEN * len_step, plot_height);
 
            for (i = CHANNEL_LEN; i > 0; i--)
            {
                n = (bytes.readFloat() * plot_height);
                g.lineTo(i * len_step, plot_height - n);
            }
            g.lineTo(0, plot_height);
            //g.endFill();
        }
    }
}

使用的时候这样做就可以了
//波形视觉效果
  var Visual:Visualization= new Visualization();
  Visual.move(5,0);
  Visual.width=90;
  Visual.height=50;
  Visual.Audioswitch=true;
  this.addChild(Visual);   


再来一个简单的:
package {
    //导入类
    //Sprite是一个只有一帧的MovieClip,相对于MovieClip少了帧和场景...
    import flash.display.Sprite;
    //混合模式类
    import flash.display.BlendMode;
    //事件类
    import flash.events.*;
    //声音类
    import flash.media.Sound;
    //混音器类
    import flash.media.SoundMixer;
    //声道类
    import flash.media.SoundChannel;
    //URLRequest类
    import flash.net.URLRequest;
    //ByteArray类
    import flash.utils.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;
    //Point类(点)
    import flash.geom.Point;
    //定义类
    public class SwfdongSound extends Sprite {
        //声明用来包含line和bg的Sprite
        private var Main:Sprite=new Sprite();
        //声明用来画线的Sprite
        private var line:Sprite=new Sprite();
        //声明用来放位图数据的BitmapData
        private var bmpData:BitmapData=new BitmapData(350,200,true,0xFF0);
        //声明用来显示效果的Bitmap
        private var bmp:Bitmap=new Bitmap(bmpData);
        //声明MP3路径
        private var url:String = "MySound.mp3";
        //声明一个Sound对象
        private var DongSound:Sound = new Sound();
        //声明一个SoundChannel对象
        private var sChannel:SoundChannel;
        //声明一个ByteArray对象
        private var bArray:ByteArray = new ByteArray();
        //声明一个数组对象
        private var Ary:Array;
        //声明两个数字对象
        private var n:Number = 0;
        private var c:Number = 0;
        //声明一个ColorMatrix滤镜
        private 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滤镜
        private var blur:BlurFilter = new BlurFilter(7,7,BitmapFilterQuality.LOW);
        //声明一个矩形
        private var r:Rectangle=new Rectangle(0,0,350,200);
        //声明一个点
        private var p:Point=new Point(0,0);
        //类构造函数
        public function SwfdongSound() {
           
            //Main的混合模式为"添加"
            Main.blendMode=BlendMode.ADD;
            //在舞台上显示各个部分
            Main.addChild(bmp);
            Main.addChild(line);
            addChild(Main);
            //将字符串转化为URLRequest
            var req:URLRequest = new URLRequest(url);
            //加载歌曲
            DongSound.load(req);
            //播放
            DongSound.play();
            //添加一个EnterFrame的侦听器,同以前的this.onEnterFrame=showBar();
            this.addEventListener(Event.ENTER_FRAME,showBar);
    }
private function showBar(event:Event){
        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();
        //设置线条样式,颜色湖蓝,宽度1,透明度100
        line.graphics.lineStyle(1,0x2AEAEB,100);
        //将当前声音输出为ByteArray,注意哦,这次用的是false,上次是true
        SoundMixer.computeSpectrum(bArray,false,0);
        for(var i=0; i < 256; i+=2){
                //在ByteArray中读取一个32位的单精度浮点数(这个是livedoc上写的,实际就是把数据流读取成浮点数)
                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);
                }
        }
     } 
   }
}

更多神奇的是:
呵呵,又是一个AS3编写的波形效果.很COOOOOOL.至于有多酷,就得自己评定了.看这里:
效果1:

效果2:

效果3:

效果4:

效果5:

 

如果访问有问题.可以观看作者BLOG:
http://www.anttikupila.com/flash/revolt-actionscript-3-based-spectrum-analyzer-source-released/
作者同时还把源文件无私的奉献出来.在这里先表示感谢.
方便观看,我把源演示地址贴出: 点这里观看
源文件下载: 点这里下载
我还收藏了一个波形效果,出自lab.andre-michelle.com:
效果图:

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liuchang5/archive/2008/12/31/3672054.aspx

你可能感兴趣的:(【FLEX】实现音频播放的波浪效果【转】)