您的位置:首页 > 其它

flex 播放mp3

2007-06-04 10:22 302 查看
研究类flex 中的声音类,做了个播放mp3的工具呵呵,现在把源码贴出来。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="OnClick()">
<mx:Script>
<![CDATA[
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);
}

]]>
</mx:Script>

<mx:Canvas x="0" y="0" width="256" height="84" id="cns" enterFrame="onEnterFrame()" click="OnCnsClick()">
</mx:Canvas>
<mx:Button x="0" y="92" label="start" click="OnClick()"/>
<mx:Button x="196" y="92" label="stop" click="OnStopClick()"/>
<mx:Button x="96" y="92" label="pause" click="OnPauseClick()"/>
<mx:HSlider x="10" y="122" height="12" width="237" minimum="0" maximum="2" id="hsldLeft" change="OnChangeVolumn()" liveDragging="true"/>

</mx:Application>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: