zoukankan      html  css  js  c++  java
  • AS3版MP3播放器核心

    代码

    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;
                    }
                   
    // 处理错误用
                    private function errorHandler(e:IOErrorEvent):void
                    {
                            sound.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
                            sound 
    = null;
                    }                
            }
  • 相关阅读:
    json字符串和xml的 比较
    Python基础第十三天——sys模块、logging模块、json模块、pickle模块
    Python基础第十二天——模块的分类、时间模块、随机数模块、摘要算法模块、os模块、时间形式轮换
    Python基础第十一天——内置函数的补充、内置函数与lambda、递归
    Python基础第九天——迭代对象、 迭代器对象、生成器、三元表达式列表解析、生成器表达式
    Python基础第八天——无参装饰器、有参装饰器
    Python基础第七天——global与nonlocal、闭包函数、装饰器
    Python基础第六天——函数的使用、函数的参数、函数的返回值、函数的嵌套、命名空间、作用域、函数对象
    Python基础第五天——字符编码、文件处理、函数的使用
    Python基础第四天——字典、布尔值、集合
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/1614771.html
Copyright © 2011-2022 走看看