//加载网络音乐 并显示频谱 ////这个例子是加载外部的文件,本例为音乐文件,注意load方法只能接受URLRequest文件 //加载网络音乐 并显示频谱 ////这个例子是加载外部的文件,本例为音乐文件,注意load方法只能接受URLRequest文件 var request:URLRequest=new URLRequest("http://www.wangzi.net/kissrain.mp3"); //定义一个loader var loader:Loader = new Loader(); //为加载过程建立事件侦听器 loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress); //加载完成的事件侦听器 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); //加载过程中执行 function loadProgress(event:ProgressEvent):void { var percentLoaded:Number=event.bytesLoaded/event.bytesTotal; percentLoaded=Math.round(percentLoaded*100); txt.text="音乐已缓冲 "+percentLoaded+"%"+""; } //加载完成后执行 function loadComplete(event:Event):void { trace("Complete"); } //载入 loader.load(request); //放置在场景中 var shengyin:Sound=new Sound(); //用load方法把dizhi加载进来 shengyin.load(request); //控制声音:播放 shengyin.play(); var _sound:Sound=new Sound(); var _channel:SoundChannel=new SoundChannel(); var url:String="http://www.wangzi.net/kissrain.mp3"; var _request:URLRequest = new URLRequest(url); _sound.load(_request); _channel=_sound.play(); var bxzr:Sprite=new Sprite(); addChildAt(bxzr,1); var bx:Sprite=new Sprite(); bxzr.addChild(bx); var byteArray:ByteArray = new ByteArray(); var n:Number=10; addEventListener(Event.ENTER_FRAME,yx); function yx(event:Event):void { bx.graphics.clear(); SoundMixer.computeSpectrum(byteArray,true,1);//将当前声音输出为ByteArray for (var i=0; i <200; i=i+5) { n = byteArray.readFloat()*60;//把数据流读取成浮点数并扩大其值 bx.graphics.lineStyle(3,0xFFFFFF,1,true,"noSacle","none"); bx.graphics.moveTo(27+i,50); bx.graphics.lineTo(27+i,50-n) bx.graphics.lineStyle(3,0xFFFFFF,0.2,true,"noSacle","none"); bx.graphics.lineTo(27+i,50+n); } } //////////////////////////////////////////////////////////////////////////////////////// 另一种频谱效果import flash.display.Graphics; import flash.events.Event; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundMixer; import flash.net.URLRequest; const PLOT_HEIGHT:int=200; const CHANNEL_LENGTH:int=256; var snd:Sound = new Sound(); var req:URLRequest=new URLRequest("http://www.wangzi.net/kissrain.mp3"); snd.load(req); var channel:SoundChannel; channel=snd.play(); addEventListener(Event.ENTER_FRAME, onEnterFrame); snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); var bytes:ByteArray = new ByteArray(); function onEnterFrame(event:Event):void { SoundMixer.computeSpectrum(bytes, false, 0); var g:Graphics=this.graphics; g.clear(); g.lineStyle(0, 0xFF0000); g.beginFill(0xFF0000); g.moveTo(0, PLOT_HEIGHT); var n:Number=0; // left channel for (var i:int = 0; i < CHANNEL_LENGTH; i++) { n = (bytes.readFloat() * PLOT_HEIGHT); g.lineTo(i * 2, PLOT_HEIGHT - n); } g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); g.endFill(); // right channel g.lineStyle(0, 0xCC0066); g.beginFill(0xCC0066, 0.5); g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); for (i = CHANNEL_LENGTH; i > 0; i--) { n = (bytes.readFloat() * PLOT_HEIGHT); g.lineTo(i * 2, PLOT_HEIGHT - n); } g.lineTo(0, PLOT_HEIGHT); g.endFill(); } function onPlaybackComplete(event:Event) { removeEventListener(Event.ENTER_FRAME, onEnterFrame); }