分类:
2010-01-05 13:41:35
import flash.media.*;
import mx.events.SliderEvent ;
private var _type:int=2;
private var _sound:Sound;
private var _channel:SoundChannel;
private var posNum:Number=0;
private var _spectrumGraph:BitmapData = new BitmapData(256, 60,
true,
0x000000aa);
//暂停
private function OnPauseClick():void{
posNum=_channel.position;
_channel.stop();
}
//停止
private function OnStopClick():void{
posNum=0;
_channel.stop();
}
private function OnClick():void{
hsldLeft.value=1;
//var spe:Spectrum=new Spectrum();
// Create bitmap for spectrum display
// pnlFrame.addEventListener(Event.ENTER_FRAME, onEnterFrame);
_sound = new Sound(new URLRequest("assets/song.mp3"));
_channel = _sound.play( posNum );
}
//输出图形
public function onEnterFrame():void
{
// Create the byte array and fill it with data
var spectrum:ByteArray = new ByteArray( );
SoundMixer.computeSpectrum(spectrum);
// Clear the bitmap
_spectrumGraph.fillRect(_spectrumGraph.rect,
0x00000000);
// Create the left channel visualization
var i:int;
if(2==_type){
for(i=0;i<256;i++) {
_spectrumGraph.setPixel32(i,
35 + spectrum.readFloat( ) * 20,
0xffffffff);
}
}
if(1==_type){
for(i=0;i<64;i++) {
_spectrumGraph.fillRect(new Rectangle(4*i,50-spectrum.readFloat( ) *
50 ,4,spectrum.readFloat( ) * 50 ),0xffffffff);
}
}
cns.graphics.beginBitmapFill(_spectrumGraph);
cns.graphics.drawRect(0,0,256,60);
cns.graphics.endFill();
var curPos:Number=_channel.position;
var curLen:Number=_sound.length;
cns.graphics.beginFill(0x0);
cns.graphics.drawRect(0,80,256,10);
cns.graphics.endFill();
cns.graphics.beginFill(0xa0);
cns.graphics.drawRect(0,80,256*curPos/curLen,10);
cns.graphics.endFill();
}
//改变输出图形的
private function OnCnsClick():void{
_type--;
if (0>=_type){
_type=2;
}
}
//改变声音大小
private function OnChangeVolumn( ):void{
var transform:SoundTransform = _channel.soundTransform ;
transform.volume =hsldLeft.value;
_channel.soundTransform = transform;
trace("_channel.leftPeak="+_channel.leftPeak*100 +" "+ "_channel.rightPeak="+_channel.rightPeak*100
+"_channel.soundTransform.volume"+_channel.soundTransform.volume*100);
}
]]>