MP3 Actionscript 3.0 核心代码实现

/**
* 媒体播放器简易版
* 主要用来实现音乐的播放,暂停,和停止。
* @author 猫粮
* @version 0.1
*/

package  {
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundLoaderContext;
        import flash.media.SoundMixer;

        public class MediaPlayerCore {
                private var sound:Sound;
                private var soundCh:SoundChannel;
                private var soundCon:SoundLoaderContext;
                private var position:Number;
                private var isPlaying:Boolean;
                private var isPause:Boolean;
                private static var BUFFERTIME:Number = 10000;
                        
                public function MediaPlayerCore()
                {
                        isPause = false;
                        isPlaying = false;
                        SoundMixer.bufferTime = BUFFERTIME; 
                }
                                /**
                                 * 创建一个声音对象
                      * @param url 媒体地址
                      * @param playNow 是否马上播放,默认为真      
                      */   
                public function createSound(url:String,playNow:Boolean = true):void
                {                        
                        dispose();
                        sound = new Sound();
                        sound.load(new URLRequest(url));
                        sound.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);             
                        if(playNow)
                                play();                            
                }
                /**
                                 * 播放
                      * @param offset 声音从哪开始      
                      */ 
                public function play(offset:Number = 0):void
                {
                        if(isPause)
                                soundCh = sound.play(position);
                        else
                                soundCh = sound.play(offset);
                        isPlaying = true;
                        isPause = false;
                }
                /**
                                 * 暂停        
                      */ 
                public function pause():void
                {
                        if(isPlaying)
                        {                        
                                position = soundCh.position;
                                stop();        
                                isPause = true;
                        }
                }
                /**
                                 * 停止        
                      */ 
                public function stop():void
                {
                        if(isPlaying)
                        {
                                soundCh.stop();
                                isPlaying = false;                                
                        }
                }
                /**
                                 * 播放位置        
                      */ 
                public function get Position():Number
                {
                        if(soundCh == null)
                                return 0;                    
                        return Math.round(soundCh.position);
                }
                /**
                                 * 声音对象长度        
                      */
                public function get Length():Number
                {
                        if(sound == null)
                                return 0;
                        return Math.round(sound.length*sound.bytesTotal/sound.bytesLoaded);
                }
                /**
                                 * 声音对象总共字节        
                      */
                public function get BytesTotal():Number
                {
                        if(sound == null)
                                return 0;
                        return sound.bytesTotal;
                }
                /**
                                 * 声音对象加载字节        
                      */
                public function get BytesLoaded():Number
                {
                        if(sound == null)
                                return 0;
                        return sound.bytesLoaded;
                }
                /**
                                 * 设置缓冲时间        
                      */
                public function set BufferTime(time:Number):void
                {
                        SoundMixer.bufferTime=time;
                }
                /**
                                 * 中途换歌的时候用的
                                 */
                private function dispose():void
                {
                        if(sound == null)
                                return ;
                        if(sound.isBuffering)
                                sound.close();
                        stop();                 
                        sound = null;
                }
                /**
                                 * 处理错误用
                * 出现了错误后sound的isbuffering仍然为真...
                * 由于我程序的逻辑,只能这样做- -
                                */
                private function errorHandler(e:IOErrorEvent):void
                {
                        sound.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
                        sound = null;
                }                
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值