VU meter flash on 2011-2-15
VU meter integration com.bit.componnents
/**
* Copyright davinci_leonard ( http://wonderfl.net/user/davinci_leonard )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9waE
*/
// Implémentation VU Meter with mp3 music | com.bit.componnents
// By Davinci_Leonard
package
{
import com.bit101.components.*;
import com.bit101.components.Meter;
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.media.*;
import flash.net.*;
import flash.filters.DropShadowFilter;
import flash.system.LoaderContext;
public class MeterDemo extends Sprite {
public const CHANNEL_LENGTH:int = 256;
private var meter:Meter;
private var label:Label;
private var label2:Label;
private var label3:Label;
private var meter2:Meter;
private var lite:IndicatorLight;
private var lite2:IndicatorLight;
private var _myTimer:Timer;
private var fichierMP3:URLRequest;
private var canal:SoundChannel;
private var conteneurSon:Sound;
private var DataLeft:*;
private var DataRight:*;
private var spectrum:ByteArray;
private var pausePosition:int;
private var bouton1:*;
private var bouton2:*;
private var n:Number =0;
private var progressbar:ProgressBar;
private var buf1:*;
private var buf2:*;
private var buffered:*;
private var listeDesFiltres:Array;
private var ombreLegere:DropShadowFilter;
private var r:RegExp;
private var s:String;
private var maxPeakL:* = 0;
private var maxPeakR:* = 0;
private var maxMax:Number = 0;
private var context:SoundLoaderContext;
public var _maximum:Number = 100.0;;
private var fond0:Sprite;
public function MeterDemo()
{
// init
stage.scaleMode = StageScaleMode.NO_SCALE;
//stage.align = StageAlign.TOP_LEFT;
//Component.initStage(stage); // initialisation des composants
TimerExample(); // declencher le timer
// filtres
listeDesFiltres = new Array();
ombreLegere = new DropShadowFilter();
ombreLegere.distance = 5;
ombreLegere.alpha = 0.7;
ombreLegere.inner = true;
ombreLegere.angle = 45;
ombreLegere.blurX = ombreLegere.blurY = 8;
listeDesFiltres.push(ombreLegere);
// creation et ajout des objets
// ajout image de fond de la bibliotheque
var sp : Sprite = new Sprite();
//sp.graphics.lineStyle(0, 0x995500);
sp.graphics.beginFill(0x333333);
sp.graphics.drawRect(0, 0, 490, 490);
sp.graphics.endFill();
sp.graphics.moveTo(0, 0);
sp.graphics.lineTo(0, 0);
sp.x = 0;
sp.y = 0;
addChild(sp);
// ajout image 2 de fond de la bibliotheque
var sp2 : Sprite = new Sprite();
//sp.graphics.lineStyle(0, 0x995500);
sp2.graphics.beginFill(0x3000000);
sp2.graphics.drawRect(0, 0, 490, 200);
sp2.graphics.endFill();
sp2.graphics.moveTo(0, 0);
sp2.graphics.lineTo(0, 0);
sp2.x = 0;
sp2.y = 0;
addChild(sp2);
meter = new Meter(this, 40, 40, "Left");
meter2 = new Meter(this, 250, 40, "Right");
meter.maximum=100;
meter2.maximum=100;
label = new Label(this, 132, 155, "0");
label2 = new Label(this, 342, 155, "0");
label3 = new Label(this, 240, 155, "max");
bouton1 = new PushButton(stage, 200, 210, "Play", onClick);
bouton2 = new PushButton(stage, 200, 210, "Pause", onClick2);
stage.addChild(bouton1);
lite = new IndicatorLight(this, 134, 145, 0xff0000, "");
lite2 = new IndicatorLight(this, 344, 145, 0xff0000, "");
meter.filters = listeDesFiltres; // appliquer les filtres
meter2.filters = listeDesFiltres; // appliquer les filtres
progressbar = new ProgressBar(this, 198, 175);
progressbar.maximum = 100;
//progressbar.value = 0;
addChild(progressbar);
// var barleft = new Sprite();
//addChild(barleft);
// création du conteneur du son
context = new SoundLoaderContext(10,true);
conteneurSon = new Sound();
var req:URLRequest = new URLRequest("http://www.project-nya.jp/images/bgm/spring.mp3");
conteneurSon.load(req,context);
canal = new SoundChannel();
canal = conteneurSon.play();
pausePosition = canal.position;
canal.stop();
// contrôle du chargement mp3
conteneurSon.addEventListener( Event.OPEN, chargementDemarre );
conteneurSon.addEventListener( Event.ID3, informationsID3 );
conteneurSon.addEventListener( ProgressEvent.PROGRESS, chargementEnCours );
conteneurSon.addEventListener( Event.COMPLETE, chargementTermine );
conteneurSon.addEventListener( IOErrorEvent.IO_ERROR, erreurChargement );
function chargementDemarre ( pEvt:Event ):void {
//trace("chargement démarré");
}
function informationsID3 ( pEvt:Event ):void {
//trace("informations ID3");
}
function chargementEnCours ( pEvt:ProgressEvent ):void {
//trace("chargement en cours : " + pEvt.bytesLoaded + " / " + pEvt.bytesTotal );
buf1 = pEvt.bytesLoaded;
buf2 = pEvt.bytesTotal;
buffered = Math.floor(( buf1/ buf2)*100);
progressbar.value =buffered;
//trace(" je charge " + buffered);
}
function chargementTermine ( pEvt:Event ):void {
//trace("chargement terminé");
removeChild(progressbar);
}
function erreurChargement ( pEvt:IOErrorEvent ):void {
//trace("erreur de chargement");
errRemovechild();
}
// fin du controle
} // fin de function MeterDemo
// timer
private function TimerExample():void {
_myTimer = new Timer(100, 0);
_myTimer.addEventListener("timer", timerHandler);
_myTimer.start();
}
// Création des données spectre
private function timerHandler(event:TimerEvent):void {
spectrum = new ByteArray();
SoundMixer.computeSpectrum(spectrum, false, 0);
//DataLeft = Math.round(spectrum.readFloat() * 130);
//DataRight = DataLeft;
// tri des informations canal gauche et droite et peak meter
// left channel
for (var i:int = 0; i < 256; i++) {
DataLeft = (spectrum.readFloat() * 350); // 150
r = /-/g;
s = DataLeft;
DataLeft =(s.replace(r, ""));
}
// right channel
for (i = 256; i > 0; i--) {
DataRight = (spectrum.readFloat() * 350); // *150
r = /-/g;
s = DataRight;
DataRight =(s.replace(r, ""));
}
// tranmettre les données
meter.value = DataLeft;
//trace("Gauche "+DataLeft);
meter2.value = DataRight;
//trace("Droite "+ DataRight);
// peak meter
if (DataLeft > 100.00){
lite.isLit = true;
var labelvar:* = Math.round(DataLeft);
label.text =labelvar;
//
maxPeakL = label
}else{
lite.isLit = false;
}
if (DataRight > 100.00){
lite2.isLit = true;
var labelvar2:* = Math.round(DataRight);
label2.text =labelvar2;
//
maxPeakR = labelvar2;
}else{
lite2.isLit = false;
}
// -----peak max-------------------
if (maxPeakL > maxMax){
maxMax = maxPeakL;
}else{
maxMax = maxMax;
}
if (maxPeakR > maxMax){
maxMax = maxPeakR;
}else{
maxMax = maxMax;
}
trace(maxPeakR);
trace(maxPeakR);
var totMaxPeak:* = maxMax;
label3.text = totMaxPeak;
// ----------------------------------
}
// fin de création des données spectre
private function onClick(event:MouseEvent) : void {
_myTimer.start();
canal = conteneurSon.play(pausePosition);
stage.removeChild(bouton1);
canal.addEventListener(Event.SOUND_COMPLETE,finDuSon);
return;
}// end function
private function finDuSon(evt:Event):void{
_myTimer.stop();
stage.addChild(bouton1);
canal.removeEventListener(Event.SOUND_COMPLETE,finDuSon);
//trace("fini");
}
private function onClick2(event:MouseEvent) : void {
pausePosition = canal.position; // variables à zéro mais ont garde la position de lecture du mp3
canal.stop();
_myTimer.stop();
this.meter2.value = 0;
this.meter.value = 0;
stage.addChild(bouton1);
return;
}// end function
private function errRemovechild():void{
stage.removeChild(bouton2);
stage.removeChild(bouton1);
return;
}
}
}