[as3]AS3写的一个MP3音乐播放器源代码
[as3]AS3写的一个MP3音乐播放器源代码
[as3]AS3写的一个MP3音乐播放器源代码
- 代码
- Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
- 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)
- soundsoundCh = sound.play(position);
- else
- soundsoundCh = 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;
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...