首先是国外某网站的一个简单,但最经典的波形图

---------------------------------代码分界-------------------------------------------------------

var url:String = "http://www.helpexamples.com/flash/sound/song3.mp3";
var request:URLRequest = new URLRequest(url);
var s:Sound = new Sound();
s.addEventListener(Event.COMPLETE, completeHandler);
s.load(request);
var song:SoundChannel = s.play();
song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
var ba:ByteArray = new ByteArray();

var gr:Sprite = new Sprite();
gr.x = 20;
gr.y = 200;
addChild(gr);

var time:Timer = new Timer(50);
time.addEventListener(TimerEvent.TIMER, timerHandler);
time.start();

function completeHandler(event:Event):void {
event.target.play();
}
function soundCompleteHandler(event:Event):void {
time.stop();
}
function timerHandler(event:TimerEvent):void {
SoundMixer.computeSpectrum(ba, true);
var i:int;
gr.graphics.clear();
gr.graphics.lineStyle(0, 0xFF0000);
gr.graphics.beginFill(0xFF0000);
gr.graphics.moveTo(0, 0);
var w:uint = 2;
for (i=0; i<512; i+=w) {
var t:Number = ba.readFloat();
var n:Number = (t * 100);
gr.graphics.drawRect(i, 0, w, -n);
}
}

---------------------------------代码分界(另一个柱状)-------------------------------------------

package {
    //导入类
    //Sprite是一个只有一帧的MovieClip,相对于MovieClip少了帧和场景...
     import flash.display.Sprite;
    //事件类
     import flash.events.*;
    //声音类
     import flash.media.Sound;
    //混音器类
     import flash.media.SoundMixer;
    //声音通道类(暂时这么翻吧)
     import flash.media.SoundChannel;
    //URLRequest类
     import flash.net.URLRequest;
    //二进制数组类
     import flash.utils.ByteArray;
    //定义类
    public class SwfdongSound extends Sprite {
        //声明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;
        //初始化函数
        public function SwfdongSound() {
            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;
        //清除绘图
        this.graphics.clear();
        //将当前声音输出为ByteArray
         SoundMixer.computeSpectrum(bArray,true,0);
        for(var i=0; i < 256; i=i+16){
                //在ByteArray中读取一个32位的单精度浮点数(这个是livedoc上写的,实际就是把数据流读取成浮点数)
                 n = bArray.readFloat();
                //这个实际作用是把n扩大一下
                var num:Number = n*360;
                //设置线条样式,颜色湖蓝,宽度10,透明度100
                this.graphics.lineStyle(10,0x99FF00,100,false,"noSacle","none");
                //移动到x坐标50+i,y坐标200的位置
                this.graphics.moveTo(50+i,200);
                //向上画图
                this.graphics.lineTo(50+i,200-num/5);
        }
  
      }  
    }
}

---------------------------------代码分界-------------------------------------------------------

posted on 2010-01-22 13:23  果冻冰冰  阅读(988)  评论(0编辑  收藏  举报