只实现了一首歌的功能,音量控制,暂停等功能已经实现。
package { import flash.display.MovieClip; import flash.media.Sound; import flash.media.SoundChannel; import flash.net.URLRequest; import flash.events.MouseEvent; import fl.events.SliderEvent; import flash.media.SoundTransform; import flash.events.ProgressEvent; import flash.events.Event; public class MP3Main extends MovieClip { private var sound:Sound; private var soundchannel:SoundChannel; private var position:Number = 0; private var soundtransform:SoundTransform; private var jiazaitiao:JiaZaiTiao; private var jiazaikuang:JiaZaiKuang; public function MP3Main() { sound = new Sound(); soundchannel = new SoundChannel(); soundtransform = new SoundTransform(); jiazaitiao = new JiaZaiTiao(); jiazaikuang = new JiaZaiKuang(); jiazaitiao.scaleX = 0; jiazaikuang.scaleX = 0; jiazaitiao.x = 197; jiazaitiao.y = 362; xiayishou.label = "上一首"; shangyishou.label = "下一首"; btn1.label = "播放"; btn2.label = "停止"; slider.value = 10; addChild(jiazaitiao); addChild(xiayishou); addChild(shangyishou); sound.load(new URLRequest("Lenka-Trouble Is A Friend.mp3")); slider.addEventListener(SliderEvent.CHANGE,onSliderHandler); btn1.addEventListener(MouseEvent.CLICK,onClickHandler); btn2.addEventListener(MouseEvent.CLICK,onStopHandler); sound.addEventListener(ProgressEvent.PROGRESS,onJiaZaiKuangHandler); sound.addEventListener(Event.COMPLETE,onJiaZaiTiaoHandler); stage.addEventListener(Event.ENTER_FRAME,onEventHandler); } private function onSliderHandler(e:SliderEvent):void { soundtransform.volume = (e.target.value)* 0.2; soundchannel.soundTransform = soundtransform; } private function onClickHandler(e:MouseEvent):void { if (btn1.label == "播放") { soundchannel = sound.play(position); btn1.label = "暂停"; } else { position = soundchannel.position; soundchannel.stop(); btn1.label = "播放"; } } private function onStopHandler(e:MouseEvent):void { soundchannel.stop(); btn1.label = "播放"; position = 0; } private function onJiaZaiKuangHandler(e:ProgressEvent):void { jiazaikuang.scaleX = e.bytesLoaded / e.bytesTotal; } private function onJiaZaiTiaoHandler(e:Event):void { if (btn1.label == "播放") { soundchannel = sound.play(); } soundchannel.stop(); } private function onEventHandler(e:Event):void { jiazaitiao.scaleX = soundchannel.position / sound.length; } }}