package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray; //字节数组类
public class Main extends Sprite
{
private var url:String = "music.mp3";
private var sound:Sound = new Sound(); //创建声音
private var schannel:SoundChannel; //创建声音通道
private var barray:ByteArray = new ByteArray(); //创建字节数组
private var ary:Array;
private var n:Number = 0; //记录数量
public function Main():void
{
init();
}
private function init():void
{
//将mp3文件载入到声音并播放
var req:URLRequest = new URLRequest(url);
sound.load(req);
sound.play();
//从载入第一帧开始,就不断循环触发的事件
this.addEventListener(Event.ENTER_FRAME, show);
}
//开始绘图
private function show(evt:Event):void
{
n = 0; //先清零
this.graphics.clear(); //清除所有图形
//把音频数据转化为字节数组,并保存在barray里,之后将会用这些数据生成动态变化的小竖线
SoundMixer.computeSpectrum(barray,true,0);
//绘制动态的小竖线动画,让每条线都隔一点距离,所以用i+=16
for(var i:uint = 0; i < 100; i += 5)
{
n = barray.readFloat();
var num:Number = n * 100;
this.graphics.lineStyle(3, 0x00ff00);
this.graphics.moveTo(i, 100); //绘制线条起始点
this.graphics.lineTo(i, 100 - num); //绘制线条终点
this.graphics.endFill();
}
}
}
}
{
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray; //字节数组类
public class Main extends Sprite
{
private var url:String = "music.mp3";
private var sound:Sound = new Sound(); //创建声音
private var schannel:SoundChannel; //创建声音通道
private var barray:ByteArray = new ByteArray(); //创建字节数组
private var ary:Array;
private var n:Number = 0; //记录数量
public function Main():void
{
init();
}
private function init():void
{
//将mp3文件载入到声音并播放
var req:URLRequest = new URLRequest(url);
sound.load(req);
sound.play();
//从载入第一帧开始,就不断循环触发的事件
this.addEventListener(Event.ENTER_FRAME, show);
}
//开始绘图
private function show(evt:Event):void
{
n = 0; //先清零
this.graphics.clear(); //清除所有图形
//把音频数据转化为字节数组,并保存在barray里,之后将会用这些数据生成动态变化的小竖线
SoundMixer.computeSpectrum(barray,true,0);
//绘制动态的小竖线动画,让每条线都隔一点距离,所以用i+=16
for(var i:uint = 0; i < 100; i += 5)
{
n = barray.readFloat();
var num:Number = n * 100;
this.graphics.lineStyle(3, 0x00ff00);
this.graphics.moveTo(i, 100); //绘制线条起始点
this.graphics.lineTo(i, 100 - num); //绘制线条终点
this.graphics.endFill();
}
}
}
}