21视频教程网3月28日整理
var myurl=new URLRequest;
var mytimer:uint;
var bit:Array = new Array(36);//采样数组
var prebit:Array = new Array(36);//预采样数组
var vt:Array = new Array(36);//下落速度数组
var peak:Array = new Array(36);//峰值位置数组
var ratios:Array = new Array(255,255)
var alphas:Array = new Array(100,100)
var matrix:Matrix = new Matrix(50,50,0,100,200,0);
var color = createRainbowGradientArray();
for(var i=0;i<36;i++) {
bit[i]=0;prebit[i]=0;vt[i]=0;peak[i]=0;
}
var bArray:ByteArray = new ByteArray();
myurl.url="song.mp3";//此处替换为您自己的mp3地址
var mysound=new Sound(myurl);
mysound.play();
setInterval(showBar,20);
mytimer=setInterval(caiyang,80);
//clik.addEventListener(MouseEvent.CLICK,clikme);
function clikme(Event:MouseEvent) {
}
function showBar() {
this.graphics.clear()
for(i=0;i<18;i++) {
if(bit[i]<prebit[i]) {
bit[i]=prebit[i];
}
if(peak[i]<bit[i]) {
peak[i]=bit[i];
vt[i]=0;
}
if(bit[i+18]<prebit[i+18]) {
bit[i+18]=prebit[i+18];
}
if(peak[i+18]<bit[i+18]) {
peak[i+18]=bit[i+18];
vt[i+18]=0;
}
this.graphics.lineStyle(10,0xFFF001,20,false,"noSacle","none");
this.graphics.lineGradientStyle("linear",new Array(color[i*14+14],color[i*14+14]),alphas,ratios,matrix,"reflect","LinearRGB",1)
this.graphics.moveTo(50+i*15,200);
this.graphics.lineTo(50+i*15,200-bit[i]/2)
this.graphics.lineGradientStyle("linear",new Array(color[i*14+14],color[i*14+14]),alphas,ratios,matrix,"reflect","LinearRGB",1)
this.graphics.moveTo(600-i*15,200);
this.graphics.lineTo(600-i*15,200-bit[i+18]/2)
bit[i]-=4;
bit[i+18]-=4;
vt[i]+=0.1;
vt[i+18]+=0.1;
if(peak[i]>0) {
peak[i]-=vt[i];
this.graphics.lineStyle(2,0xCCCCCC,100,false,"noScale","none")
this.graphics.moveTo(45+i*15,200-peak[i]/2);
this.graphics.lineTo(55+i*15,200-peak[i]/2);
}
if(peak[i+18]>0) {
peak[i+18]-=vt[i+18];
this.graphics.lineStyle(2,0xCCCCCC,100,false,"noScale","none")
this.graphics.moveTo(595-i*15,200-peak[i+18]/2);
this.graphics.lineTo(605-i*15,200-peak[i+18]/2);
}
}
}
function caiyang() {
SoundMixer.computeSpectrum(bArray,true,0);
for(var i=0;i<18;i++) {
for(var j=0;j<30;j++) {
prebit[i]+=bArray[i*50+j+1]*2^j
prebit[i+18]+=bArray[1024+i*50+j]*2^j
}
prebit[i]/=50
prebit[i+18]/=50
}
}
function createRainbowGradientArray(): Array
{
var gradient: Array = new Array();
var shape: Shape = new Shape();
var bmp: BitmapData = new BitmapData( 256, 1, false, 0 );
var colors: Array = [ 0, 0xff0000, 0xffff00, 0x00ff00, 0x00ffff ];
var alphas: Array = [ 100, 100, 100, 100, 100 ];
var ratios: Array = [ 0, 16, 128, 192, 255 ];
var matrix: Matrix = new Matrix();
matrix.createGradientBox( 256, 1, 0, 0, 0 );
shape.graphics.beginGradientFill( 'linear', colors, alphas, ratios, matrix );
shape.graphics.drawRect( 0, 0, 256, 1 );
shape.graphics.endFill();
bmp.draw( shape );
for( var i: int = 0 ; i < 256 ; i++ )
{
gradient[i] = bmp.getPixel( i, 0 );
}
return gradient;
}
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);
}
}
}
}
}
Flash教程
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |