Current location - Music Encyclopedia - NetEase Cloud Music - What function can be used in AS3 to realize the sound waveform that changes with the music rhythm?
What function can be used in AS3 to realize the sound waveform that changes with the music rhythm?
The general principle is that after you load a song, play () registers an enterframe method. In this method, soundmixer is used. ComputeSpectrum (barray,true,0); //Convert audio data into byte array and save it in barray (SoundMixer class will automatically get the sound stream in the program). Then use binarybaray.readfloat () to get the sound data of each frame, and do what you want according to this sound data. Let me give you a simple example.

Common class sound pull rod extension Sprite

{

Private var URL: string = "a.mp3";

Private varsound: sound = newsound (); //Create a sound

Private varschannel: soundchannel; //Create a sound channel

private var barray:ByteArray = new ByteArray(); //Create a byte array

Private variable: array;

Private var n:Number = 0;; //Number of records

Private var line: Sprite = New Sprite; //row container

Common function void pull rod (): void

{

//var sound test:sound test = new sound test(stage);

//addChild(sound test);

init();

}

Private function init():void

{

//Load mp3 files into sound and play them.

var req:URL request = new URL request(URL);

Sound.load (request);

sound . play();

this . addchild(lines);

//Since the first frame is loaded, the triggered events are constantly circulating.

This.addEventListener (event. ENTER_FRAME,show);

}

//Start drawing

Private function display (evt:Event):void

{

if(math . random()& gt; 0.5){

var r:int =-Math.random()*20

} Otherwise {

r = Math.random()*20

}

n = 0; //Clear first

lines . graphics . clear(); //Clear all graphics

//Convert audio data into byte arrays and save them in barray, and then use these data to generate dynamic small vertical lines.

sound mixer . computespectrum(barray,true,0);

var tempx:int = 0;

var tempy:int = 100;

//Draw a dynamic animation of small vertical lines, so that each line is separated by a little distance, so i+= 16 is used.

for(var I:uint = 0; I & lt400; i += 10)

{

n = barray . read float();

var num:Number = n * 100;

lines.graphics.lineStyle(4,0x 00ff 00);

lines.graphics.moveTo(tempx,tempy); //Draw the line starting point

lines.graphics.moveTo(i, 100);

lines.graphics.lineTo(i, 100-num); //Draw the line end point

lines . graphics . end fill();

tempx = I;

tempy = 100-num;

}

}

}

In fact, the most important thing is that you get barray's data, and everything made according to this data can have the effect of following the fluctuation of music.