标题: 用flash9制作的播放音乐时频谱真实显示
ljjk5
元帅
Rank: 1


荣誉会员奖章
UID 46706
精华 1
积分 99426
帖子 49690
威望 554
金币 48489
热心 505
阅读权限 100
注册 2007-2-25
状态 离线
用flash9制作的播放音乐时频谱真实显示


(需要首先将mp3转换为swf,再对转换后的swf进行处理,才能正确显示声音频谱),可以说Flash8及以下的版本对声音的支持是有限的,而Flash9在此方面进行了升级,对声音的处理能力大为提升,下面发一个用flash9制作的播放音乐时频谱真实显示的小实例(参考了他人心得),界面如下图1:

1.打开Flash9,在主场景第一帖上(仅此一帧):
import flash.display.Sprite;   
import flash.display.BlendMode;  //混合模式类  
import flash.events.*;   //事件类
import flash.media.Sound;    //声音类
import flash.media.SoundMixer;  //混音器类  
import flash.media.SoundChannel; //声道类   
import flash.net.URLRequest;    //URLRequest类
import flash.utils.ByteArray;   //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;    //矩形类
import flash.geom.Point;  //Point类(点)
Stage.showDefaultContextMenu = false;
        //声明用来包含line和bg的Sprite
var Main:Sprite=new Sprite();
        //声明用来画线的Sprite
var line:Sprite=new Sprite();
        //声明用来放位图数据的BitmapData
var bmpData:BitmapData=new BitmapData(350,200,true,0xFF0000);
        //声明用来显示效果的Bitmap
var bmp:Bitmap=new Bitmap(bmpData);
        //声明一个ByteArray对象
var bArray:ByteArray = new ByteArray();
        //声明一个数组对象
var Ary:Array;
        //声明两个数字对象
var n:Number = 0;
var c:Number = 0;
        //声明一个ColorMatrix滤镜
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滤镜
var blur:BlurFilter = new BlurFilter(7,7,BitmapFilterQuality.LOW);
        //声明一个矩形
var r:Rectangle=new Rectangle(0,0,350,200);
        //声明一个点
var point=new Point(0,0);      
function showBar1(event:Event):void{
        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();
        //设置线条样式,颜色green,宽度1,透明度100
        line.graphics.lineStyle(1,0x00CC00,100);
        //将当前声音输出为ByteArray,注意哦,这次用的是false,上次是true
        SoundMixer.computeSpectrum(bArray,false,0);
        for(var i=0; i<256; i+=2){
                //在ByteArray中读取一个32位的单精度浮点数
                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);
    }
  }
}
//
play_btn.addEventListener(MouseEvent.MOUSE_DOWN,playSounds);
stop_btn.addEventListener(MouseEvent.MOUSE_DOWN,stopSounds);
pause_btn.addEventListener(MouseEvent.MOUSE_DOWN,pauseSounds);
var kaiguan:Boolean =false;
function playSounds(event:MouseEvent):void {
   if(kaiguan){
    _channel = _sound.play(_position);
kaiguan=false;
}
}
function stopSounds(event:MouseEvent):void {
    //....设置为0位置才能使进度条归零.....
_channel = _sound.play(0);
//......停止声音的方法........
SoundMixer.stopAll( );
kaiguan=true;
}
function pauseSounds(event:MouseEvent):void {
    if(!kaiguan) {
      _position = _channel.position;
      _channel.stop( );
   kaiguan = !kaiguan;
  }           
}
//
var _position:int =0;
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void{
     //..........进度条初始值.........................
    var barWidth:int = 271;
    var barHeight:int = 5;
    var loaded:int = _sound.bytesLoaded;
    var total:int = _sound.bytesTotal;            
    var length:int = _sound.length;
    _position = _channel.position;
    //................ 画进度条背景....................................
    graphics.clear(  );
    graphics.beginFill(0x333333);
    graphics.drawRect(40, 187, barWidth, barHeight);
    graphics.endFill(  );
//..................................................................
    //.............时间显示设置区................................................
var miao:int=Math.round(length/1000)-Math.round(length/60000)*60;
var fen:int=Math.round(length/60000);
if(fen<10){
  if(Math.abs(miao)<10){
   musicTimer.text="0"+fen+":0"+ Math.abs(miao);
  }else{
  musicTimer.text="0"+fen+":"+ Math.abs(miao);}
  }else{
   if(Math.abs(miao)<10){
    musicTimer.text=fen+":0"+ Math.abs(miao);
    }else{
     musicTimer.text=fen+":"+ Math.abs(miao);
  }
}
var playmiao:int=_position/1000;
if(playmiao<60){
  if(playmiao<10){
     musicPlayingTimer.text="00:0"+playmiao;
  }else{
   musicPlayingTimer.text="00:"+playmiao;
   }
     }
if(playmiao>=60&&playmiao<120){
  if(Number(playmiao-60)<10){
  musicPlayingTimer.text="01:0"+Number(playmiao-60);
  }else{
   musicPlayingTimer.text="01:"+Number(playmiao-60);
  }
  }
if(playmiao>=120&&playmiao<180){
  if(Number(playmiao-120)<10){
  musicPlayingTimer.text="02:0"+Number(playmiao-120);
  }else{
  musicPlayingTimer.text="02:"+Number(playmiao-120);
  }
  }
if(playmiao>=180&&playmiao<240){
  if(Number(playmiao-180)<10){
  musicPlayingTimer.text="03:0"+Number(playmiao-180);
  }else{
  musicPlayingTimer.text="03:"+Number(playmiao-180);
  }
  }
if(playmiao>=240&&playmiao<300){
  if(Number(playmiao-240)<10){
  musicPlayingTimer.text="04:0"+Number(playmiao-240);
  }else{
  musicPlayingTimer.text="04:"+Number(playmiao-240);
  }
  }
if(playmiao>=300&&playmiao<360){
  if(Number(playmiao-300)<10){
  musicPlayingTimer.text="05:0"+Number(playmiao-300);
  }else{
  musicPlayingTimer.text="05:"+Number(playmiao-300);
  }
  }
if(playmiao>=360&&playmiao<420){
  if(Number(playmiao-360)<10){
  musicPlayingTimer.text="06:0"+Number(playmiao-360);
  }else{
  musicPlayingTimer.text="06:"+Number(playmiao-360);
  }
  }
  //...........................................................................
    if(total > 0) {
           // 已经播放声音百分比
  var percentBuffered:Number = loaded / total;
                // 画已经播放声音进度条
      graphics.beginFill(0x666666);
      graphics.drawRect(40, 187, barWidth*percentBuffered,barHeight);
      graphics.endFill(  );               
                // 改变长度
       length /= percentBuffered;
    var percentPlayed:Number = _position / length;
    graphics.beginFill(0xFFFFFF);
       graphics.drawRect(40, 187,barWidth*percentPlayed,barHeight);
       graphics.endFill(  );
      }
   var volume:Number = (音量控制.vl_mc.x /50);
      setVolume(volume);
}  
function setVolume(volume:Number):void {
     var transform:SoundTransform = _channel.soundTransform;
     transform.volume = volume;
     _channel.soundTransform = transform;
}
var _playList:Array;
var _songNameList:Array;// 歌曲列表
var _index:int = 0;       // 当前歌曲
PlayList();
function PlayList( ) {
//Main的混合模式为"添加"
Main.blendMode=BlendMode.ADD;
    //在舞台上显示各个部分
    Main.addChild(bmp);
    Main.addChild(line);
    addChild(Main);
this.addEventListener(Event.ENTER_FRAME,showBar1);
    //播放列表
    _playList = ["求佛.mp3","听妈妈的话.mp3","从开始到现在.mp3"];
_songNameList=["求佛","听 妈 妈 的 话","从开始到现在"];
playNextSong( );
}
var _channel:SoundChannel;
function playNextSong( ):void{
     if(_index < _playList.length) {
  _sound = new Sound( );
     _sound.load(new URLRequest(_playList[_index]));
  songName_txt.text=_songNameList[_index];
     _channel = _sound.play( );  
  _channel.addEventListener(Event.SOUND_COMPLETE,onComplete);
  _index++;//侦听声音是否播完
  }
if(_index>=_playList.length){
      _index=0;
   }//循环播放
}
function onComplete(event:Event):void{
     playNextSong( );
}

2.上述代码中的stop_btn、play_btn、pause_btn分别是停止、播放、暂停按钮名称,其它的就不浪费篇幅了,另外请在当前目录下放置三首上述音乐(当然你也可以放你喜欢的,只需要更改上述代码中的音乐列表数组就成了),

网友 ljjk5 签名 - 网友社区 ===
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-13 19:14
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP