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

[AS3]as3音乐播放器源代码实例

时间:2015-07-14 09:10酷播
[AS3]as3音乐播放器源代码实例

[AS3]as3音乐播放器源代码实例

  1. package { 
  2.     ////可视化相关类 
  3.     import flash.display.Sprite; 
  4.     //声音相关类 
  5.     import flash.media.Sound; 
  6.     import flash.media.SoundChannel; 
  7.     import flash.media.SoundLoaderContext; 
  8.     import flash.media.SoundMixer; 
  9.     import flash.media.SoundTransform; 
  10.     //事件类 
  11.     import flash.events.Event; 
  12.     import flash.events.IOErrorEvent; 
  13.     import flash.events.EventDispatcher; 
  14.     //加载相关类 
  15.     import flash.net.URLRequest; 
  16.     //文本框相关类 
  17.     import flash.text.TextField; 
  18.     import flash.text.TextFieldAutoSize; 
  19.      
  20.     //类及变量声明 
  21.     //======================================================================================// 
  22.     public class KingPlay extends Sprite { 
  23.         //媒体列表 
  24.         private var playList:Array; 
  25.         //声音对象 
  26.         private var playSound:Sound; 
  27.         //声音控制对象 
  28.         private var playSoundChannel:SoundChannel; 
  29.         //是否正在播放 
  30.         private var isPlay:Boolean; 
  31.         //媒体地址 
  32.         private var _playURL:String; 
  33.         //当前播放的媒体名字 
  34.         private var _playName:TextField=new TextField(); 
  35.         //匹配歌词地址 
  36.         private var _lrc:String; 
  37.         //播放媒体在列表中ID 
  38.         private var _playID:int=0
  39.         //播放头 
  40.         private var playTime:Number; 
  41.         //循环模式 
  42.         //1为列表循环(默认),2为单曲循环 
  43.         private var _cycMode:int=1
  44.         //自动播放模式 
  45.         //1为顺序播放(默认),2为随机播放 
  46.         private var _autoMode:int=1
  47.         //每次加载并播放时调用的事件 
  48.         public const LOADPALY:String="loadplay"
  49.  
  50.  
  51.         //======================================================================================// 
  52.         //构造函数 
  53.         public function KingPlay() { 
  54.             init(); 
  55.         } 
  56.         //初始化 
  57.         private function init() { 
  58.             _playName.selectable=false
  59.             _playName.autoSize=TextFieldAutoSize.LEFT; 
  60.         } 
  61.         // 
  62.         //======================================================================================// 
  63.         /** 
  64.         //提供给外部设置的属性 
  65.         **/ 
  66.         //================================================》 
  67.         //列表绑定(绑定后才真正运行) 
  68.         public function set listArray(arr:Array) { 
  69.             playList=arr
  70.             load(playList[_playID].url); 
  71.         } 
  72.         //================================================》 
  73.         //循环模式 
  74.         public function set cycMode(cycMode:int) { 
  75.             _cycMode=cycMode; 
  76.         } 
  77.         //================================================》 
  78.         //自动播放模式 
  79.         public function set autoMode(autoMode:int) { 
  80.             _autoMode=autoMode; 
  81.         } 
  82.         //   
  83.         //======================================================================================// 
  84.         /** 
  85.         //提供给外部的只读属性 
  86.         **/ 
  87.         //================================================》 
  88.         //声音对象 
  89.         public function get sound():Sound { 
  90.             return playSound; 
  91.         } 
  92.         //================================================》 
  93.         //声音控制对象 
  94.         public function get channel():SoundChannel { 
  95.             return playSoundChannel; 
  96.         } 
  97.         //================================================》 
  98.         //匹配媒体的歌词文件URL 
  99.         public function get lrc():String { 
  100.             return _lrc; 
  101.         } 
  102.         //================================================》 
  103.         //媒体在列表中的ID 
  104.         public function get playID():uint { 
  105.             return _playID; 
  106.         } 
  107.         //  
  108.         //======================================================================================// 
  109.         /** 
  110.         //提供给外部方法 
  111.         **/ 
  112.         //================================================》 
  113.         //获取媒体名(为一文本框) 
  114.         public function getPlayName():TextField { 
  115.             return _playName; 
  116.         } 
  117.         //================================================》 
  118.         //设置音量 
  119.         public function setVolume(num:Number) { 
  120.             var _volume:SoundTransform=new SoundTransform(num,0); 
  121.             playSoundChannel.soundTransform=_volume
  122.         } 
  123.         //================================================》 
  124.         //加载歌曲 
  125.         public function load(playURL:String) { 
  126.             //先停止所有声音 
  127.             SoundMixer.stopAll(); 
  128.             //新建声音 
  129.             playSound=new Sound() ; 
  130.             _playURL=playURL; 
  131.             //加载(缓冲5秒) 
  132.             playSound.load(new URLRequest(_playURL),new SoundLoaderContext(5000,true)); 
  133.             //加载错误时调用 
  134.             playSound.addEventListener(IOErrorEvent.IO_ERROR,loadErr); 
  135.             //播放声音 
  136.             playSoundplaySoundChannel=playSound.play(); 
  137.             //播放状态 
  138.             isPlay=true
  139.             //匹配歌词 
  140.             _lrc=searchLRC(_playURL); 
  141.             //当前媒体在列表的ID位置 
  142.             _playID=searchID(_playURL); 
  143.             //更新歌媒体名 
  144.             upPlayName(); 
  145.             //对外部广播加载并播放声音后的事件 
  146.             dispatchEvent(new Event(LOADPALY)); 
  147.             //声音播放完毕 
  148.             playSoundChannel.addEventListener(Event.SOUND_COMPLETE,soundComplete); 
  149.         } 
  150.         /** 
  151.         以下方法为控制媒体播放状态 
  152.         **/ 
  153.         //================================================》 
  154.         //定位到指定时间播放 
  155.         public function goto(time:Number) { 
  156.             playSoundChannel.stop(); 
  157.             playSoundplaySoundChannel=playSound.play(time); 
  158.             playSoundChannel.addEventListener(Event.SOUND_COMPLETE,soundComplete); 
  159.             isPlay=true
  160.         } 
  161.         //================================================》 
  162.         //播放 
  163.         public function play() { 
  164.             if (! isPlay) { 
  165.                 playSoundplaySoundChannel=playSound.play(playTime); 
  166.                 playSoundChannel.addEventListener(Event.SOUND_COMPLETE,soundComplete); 
  167.                 isPlay=! isPlay; 
  168.             } 
  169.         } 
  170.         //================================================》 
  171.         //暂停 
  172.         public function pause() { 
  173.             if (isPlay) { 
  174.                 playTime=playSoundChannel.position; 
  175.                 playSoundChannel.stop(); 
  176.                 isPlay=! isPlay; 
  177.             } 
  178.         } 
  179.         //================================================》 
  180.         //停止 
  181.         public function stop() { 
  182.             if (isPlay) { 
  183.                 playTime=0
  184.                 playSoundChannel.stop(); 
  185.                 isPlay=! isPlay; 
  186.             } 
  187.         } 
  188.         //================================================》 
  189.         //下一首 
  190.         public function next() { 
  191.             if (_cycMode==1) { 
  192.                 if (_autoMode==1) { 
  193.                     //顺序循环 
  194.                     if (playID<playList.length-1) { 
  195.                         playSoundChannel.stop(); 
  196.                         _playID++; 
  197.                         load(playList[_playID].url); 
  198.                     } else { 
  199.                         playSoundChannel.stop(); 
  200.                         _playID=0
  201.                         load(playList[_playID].url); 
  202.                     }//END IF 
  203.                 } else { 
  204.                     //随机循环 
  205.                     _playID=Math.floor(Math.random()*(playList.length-1)); 
  206.                     playSoundChannel.stop(); 
  207.                     load(playList[_playID].url); 
  208.                 } 
  209.             } else { 
  210.                 //单曲循环 
  211.                 playSoundChannel.stop(); 
  212.                 load(playList[_playID].url); 
  213.             } 
  214.         } 
  215.         //================================================》 
  216.         //上一首 
  217.         public function last() { 
  218.             if (playID>0) { 
  219.                 playSoundChannel.stop(); 
  220.                 _playID--; 
  221.                 load(playList[_playID].url); 
  222.             } else { 
  223.                 playSoundChannel.stop(); 
  224.                 _playID=playList.length-1; 
  225.                 load(playList[_playID].url); 
  226.             } 
  227.         } 
  228.         //  
  229.         //======================================================================================// 
  230.         /** 
  231.         以下为内部方法 
  232.         **/ 
  233.         //更新歌曲名 
  234.         private function upPlayName() { 
  235.             var tmpStr:String=playList[_playID].name.toString(); 
  236.             var len:int=tmpStr.length; 
  237.             var tmpLen:int=tmpStr.length>10?tmpLen=10:tmpStr.length; 
  238.             //取7个字符防止溢出 
  239.             tmpStr=len>10?tmpStr.substr(0,tmpLen)+" ...":tmpStr.substr(0,tmpLen); 
  240.             _playName.htmlText="<FONT color='#FFFFFF'>"+tmpStr+"</FONT>"; 
  241.         } 
  242.         //================================================》 
  243.         //匹配媒体在列表中的歌词文件 
  244.         private function searchLRC(song:String):String { 
  245.             for each (var tmp in playList) { 
  246.                 if (tmp.url==song) { 
  247.                     return tmp.lrc; 
  248.                 } 
  249.             } 
  250.             return "无歌词"; 
  251.         } 
  252.         //================================================》 
  253.         //匹配媒体在列表中的ID位置 
  254.         private function searchID(song:String):int { 
  255.             for (var id:int=0; id<playList.length; id++) { 
  256.                 if (playList[id].url==song) { 
  257.                     return id; 
  258.                 } 
  259.             } 
  260.             return 0; 
  261.         } 
  262.         //================================================》 
  263.         //媒体播放完毕后自动下一首 
  264.         private function soundComplete(e:Event) { 
  265.             trace("播放完成") 
  266.             playSoundChannel.stop(); 
  267.             next(); 
  268.         } 
  269.         //================================================》 
  270.         //加载媒体失败时,自动下一首 
  271.         private function loadErr(evt:IOErrorEvent):void { 
  272.             trace("加载歌曲错误") 
  273.             next(); 
  274.         } 
  275.     } 

 

热门文章推荐

请稍候...

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

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