A real display of the spectrum when playing music with Flash9

Source: Internet
Author: User
Tags abs filter addchild array
Show

(Need to first convert MP3 to SWF, and then processing the converted SwF to correctly display the sound spectrum), it can be said that Flash8 and the following version of The voice support is limited, and FLASH9 in this regard has been upgraded, the ability to deal with the sound greatly improved, Below is a small example of how the spectrum is actually displayed when playing music with FLASH9 (refer to other people's experience), and the interface is shown in Figure 1:

1. Open Flash9, at home scene first post (only this frame):

Import Flash.display.Sprite;
Import Flash.display.BlendMode; Mixed Mode classes
Import flash.events.*; Event class
Import Flash.media.Sound; Sound class
Import Flash.media.SoundMixer; Mixer class
Import Flash.media.SoundChannel; Channel class
Import Flash.net.URLRequest; URLRequest class
Import Flash.utils.ByteArray; ByteArray class
Import Flash.display.Bitmap; Bitmap class
Import Flash.display.BitmapData;
Import Flash.filters.BlurFilter; Filter class
Import Flash.filters.ColorMatrixFilter;
Import flash.filters.BitmapFilterQuality; Filter Quality Class
Import Flash.geom.Rectangle; Rectangular class
Import Flash.geom.Point; Point Class (Dots)
Stage.showdefaultcontextmenu = false;
Statement used to contain line and BG Sprite
var main:sprite=new Sprite ();
Declaration of sprite for line drawing
var line:sprite=new Sprite ();
BitmapData for declaring bitmap data
var bmpdata:bitmapdata=new BitmapData (350,200,true,0xff0000);
Declares the bitmap to display the effect
var bmp:bitmap=new Bitmap (Bmpdata);
Declare a ByteArray object
var barray:bytearray = new ByteArray ();
Declaring an Array object
var Ary:array;
Declaring two numeric objects
var n:number = 0;
var c:number = 0;
Declare a ColorMatrix filter
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,]);
Declare a blurfilter filter
var blur:blurfilter = new Blurfilter (7,7,bitmapfilterquality.low);
Declare a rectangle
var r:rectangle=new Rectangle (0,0,350,200);
Declare a point
var point=new point (0,0);
function ShowBar1 (event:event): void{
n = 0;
This is done in order to perform a filter every 2 times if, if you need to let the original wave diagram disappear more slowly, change 2 to a larger number
if (c%2==0) {
Draws the contents of main to Bmpdata
Bmpdata.draw (Main);
Apply Filters
Bmpdata.applyfilter (Bmpdata,r,p,colorm);
Bmpdata.applyfilter (Bmpdata,r,p,blur);
}
C + +;
Clear drawing
Line.graphics.clear ();
Set line style, color green, width 1, transparency 100
Line.graphics.lineStyle (1,0x00cc00,100);
The current sound output is bytearray, note oh, this time with false, last is true
Soundmixer.computespectrum (barray,false,0);
for (var i=0; i<256; i+=2) {
Read a 32-bit single-precision floating-point number in ByteArray
n = barray.readfloat ();
The real effect is to enlarge n
n = n*360;
If I was not 0
if (i!=0) {
Draw Wave Diagram
Line.graphics.lineTo (50+I,100-N/5);
}else{
Move
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 {
//.... Set to 0 position to zero the progress bar ....
_channel = _sound.play (0);
//...... Ways to stop sound ...
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{
//.......... Progress bar initial value ............. .....
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;
//................ Draw the background of a progress bar ....... ...... ...... ...... .....
Graphics.clear ();
Graphics.beginfill (0x333333);
Graphics.drawrect (187, BarWidth, barheight);
Graphics.endfill ();
//..................................................................
//............. Time to show the settings ... ..... ..... ..... ...... ...... ...... ...... ..... .....
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= ":" +playmiao;
}
}
if (playmiao>=60&&playmiao<120) {
if (number (playmiao-60) <10) {
musicplayingtimer.text= "01:0" +number (playmiao-60);
}else{
musicplayingtimer.text= ":" +number (playmiao-60);
}
}
if (playmiao>=120&&playmiao<180) {
if (number (playmiao-120) <10) {
musicplayingtimer.text= "02:0" +number (playmiao-120);
}else{
musicplayingtimer.text= ":" +number (playmiao-120);
}
}
if (playmiao>=180&&playmiao<240) {
if (number (playmiao-180) <10) {
musicplayingtimer.text= "03:0" +number (playmiao-180);
}else{
musicplayingtimer.text= ":" +number (playmiao-180);
}
}
if (playmiao>=240&&playmiao<300) {
if (number (playmiao-240) <10) {
musicplayingtimer.text= "04:0" +number (playmiao-240);
}else{
musicplayingtimer.text= ":" +number (playmiao-240);
}
}
if (playmiao>=300&&playmiao<360) {
if (number (playmiao-300) <10) {
musicplayingtimer.text= "05:0" +number (playmiao-300);
}else{
Musicplayingtimer.text= "A:" +number (playmiao-300);
}
}
if (playmiao>=360&&playmiao<420) {
if (number (playmiao-360) <10) {
musicplayingtimer.text= "06:0" +number (playmiao-360);
}else{
Musicplayingtimer.text= "+number" (playmiao-360);
}
}
//...........................................................................
if (Total > 0) {
Percentage of sound already played
var percentbuffered:number = loaded/total;
The picture has played the sound progress bar
Graphics.beginfill (0x666666);
Graphics.drawrect (187, barwidth*percentbuffered,barheight);
Graphics.endfill ();
Change length
Length/= percentbuffered;
var percentplayed:number = _position/length;
Graphics.beginfill (0xFFFFFF);
Graphics.drawrect (187,barwidth*percentplayed,barheight);
Graphics.endfill ();
}
var volume:number = (Volume control. 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;//song list
var _index:int = 0; Current Song
PlayList ();
function PlayList () {
The blending mode of main is "add"
Main.blendmode=blendmode.add;
Show each part on the stage
Main.addchild (BMP);
Main.addchild (line);
AddChild (Main);
This.addeventlistener (EVENT.ENTER_FRAME,SHOWBAR1);
Play List
_playlist = ["Buddha mp3", "Listen to Your mother. mp3", "from the beginning to the present. mp3"];
_songnamelist=["Seek Buddha", "Listen to Your Mother", "from the beginning to the present"];
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++;//listening for sound is finished
}
if (_index>=_playlist.length) {
_index=0;
}//Loop Playback
}
function OnComplete (event:event): void{
Playnextsong ();
}

2. The above code in the STOP_BTN, PLAY_BTN, pause_btn are stop, play, pause button name, others do not waste space, in addition to the current directory to place three of the above music (of course, you can also put your favorite, You just need to change the music list array in the code above.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.