·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[as3]AS3写的一个MP3音乐播放器源代码

时间:2015-07-08 18:33酷播
[as3]AS3写的一个MP3音乐播放器源代码

[as3]AS3写的一个MP3音乐播放器源代码

  1. 代码 
  2.  
  3. Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 
  4. package  { 
  5.         import flash.media.Sound; 
  6.         import flash.media.SoundChannel; 
  7.         import flash.media.SoundLoaderContext; 
  8.         import flash.media.SoundMixer; 
  9.  
  10.         public class MediaPlayerCore { 
  11.                 private var sound:Sound; 
  12.                 private var soundCh:SoundChannel; 
  13.                 private var soundCon:SoundLoaderContext; 
  14.                 private var position:Number; 
  15.                 private var isPlaying:Boolean; 
  16.                 private var isPause:Boolean; 
  17.                 private static var BUFFERTIME:Number = 10000
  18.                          
  19.                 public function MediaPlayerCore() 
  20.                 { 
  21.                         isPause = false
  22.                         isPlaying = false
  23.                         SoundMixer.bufferTime = BUFFERTIME;  
  24.                 } 
  25.                //创建一个声音对象 
  26.                  //@param url 媒体地址 
  27.                  // @param playNow 是否马上播放,默认为真       
  28.                 public function createSound(url:String,playNow:Boolean = true):void 
  29.                 {                         
  30.                         dispose(); 
  31.                         sound = new Sound(); 
  32.                         sound.load(new URLRequest(url)); 
  33.                         sound.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);              
  34.                         if(playNow) 
  35.                                 play();                             
  36.                 } 
  37.                 //播放 
  38.                   //@param offset 声音从哪开始       
  39.                 public function play(offset:Number = 0):void 
  40.                 { 
  41.                         if(isPause) 
  42.                                 soundsoundCh = sound.play(position); 
  43.                         else 
  44.                                 soundsoundCh = sound.play(offset); 
  45.                         isPlaying = true
  46.                         isPause = false
  47.                 } 
  48.                //暂停         
  49.                 public function pause():void 
  50.                 { 
  51.                         if(isPlaying) 
  52.                         {                         
  53.                                 position = soundCh.position; 
  54.                                 stop();         
  55.                                 isPause = true
  56.                         } 
  57.                 } 
  58.                 //停止         
  59.                 public function stop():void 
  60.                 { 
  61.                         if(isPlaying) 
  62.                         { 
  63.                                 soundCh.stop(); 
  64.                                 isPlaying = false;                                 
  65.                         } 
  66.                 } 
  67.                 //播放位置         
  68.                 public function get Position():Number 
  69.                 { 
  70.                         if(soundCh == null) 
  71.                                 return 0;                     
  72.                         return Math.round(soundCh.position); 
  73.                 } 
  74.                 //声音对象长度         
  75.                 public function get Length():Number 
  76.                 { 
  77.                         if(sound == null) 
  78.                                 return 0; 
  79.                         return Math.round(sound.length*sound.bytesTotal/sound.bytesLoaded); 
  80.                 } 
  81.                 //声音对象总共字节         
  82.                 public function get BytesTotal():Number 
  83.                 { 
  84.                         if(sound == null) 
  85.                                 return 0; 
  86.                         return sound.bytesTotal; 
  87.                 } 
  88.                 //声音对象加载字节         
  89.                 public function get BytesLoaded():Number 
  90.                 { 
  91.                         if(sound == null) 
  92.                                 return 0; 
  93.                         return sound.bytesLoaded; 
  94.                 } 
  95.                 //设置缓冲时间         
  96.                 public function set BufferTime(time:Number):void 
  97.                 { 
  98.                         SoundMixer.bufferTime=time
  99.                 } 
  100.                //中途换歌的时候用的 
  101.                 private function dispose():void 
  102.                 { 
  103.                         if(sound == null) 
  104.                                 return ; 
  105.                         if(sound.isBuffering) 
  106.                                 sound.close(); 
  107.                         stop();                  
  108.                         sound = null
  109.                 } 
  110.                // 处理错误用 
  111.                 private function errorHandler(e:IOErrorEvent):void 
  112.                 { 
  113.                         sound.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler); 
  114.                         sound = null
  115.                 }                 
  116.         } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器