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>
<?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>
相关文章推荐
- flex 播放mp3文件
- Flex mp3播放
- Flex mp3播放
- Flex播放MP3以及停止播放功能
- flex视频监控播放实时流
- MP3的播放与停止
- [j2me]利用JLayerME-CLDC0.2播放MP3的试验[1]
- MediaPlayer中mp3音乐的播放
- 车载导航不支持播放全部的mp3音乐的解决办法--------fwqlzz love is for ever
- Flex 播放器停止播放并清除屏幕(3)
- web简易MP3播放插件 Aplayer篇章一
- brew流式播放mp3
- 用多媒体库 Bass.dll 播放 mp3 [4] - 显示与设置播放进度
- Flex 设置播放GIF图片
- MyMinimad ── Linux下用libmad写的mp3解码播放程序
- 元旦快乐,阖家团圆,幸福安康.简单地播放MP3和暂停
- Flash 与课件制作:Mp3播放
- C#中用API实现MP3等音频文件的播放类(转)
- win32强化练习2.4_双击播放MP3(列表框与通知码)
- Windows Phone Page 中播放音频,mp3,wma