[AS3]as3写的音乐播放器源代码实例(Sound类实例)
[AS3]as3写的音乐播放器源代码实例(Sound类实例),as3播放器,as3音乐播放器,音乐播放器
- 1.控制库里的声音
- import flash.media.SoundChannel;
- import flash.media.Sound;
- var mysound:videoS = new videoS();
- var trans:SoundTransform = new SoundTransform(0.6,-1);
- var SSChannel:SoundChannel = new SoundChannel() ;
- //可以设置play()的looping参数;
- SSChannel = mysound.play(0,int.MAX_VALUE);//它是int类型的最大值,它等于2,147,483,647,循环这个次数至少可以停留70年。
- //停止声音
- SSChannel.stop();
- 2.监听控制
- /*
- As3Sound.as
- */
- package {
- import flash.display.Sprite;
- import flash.events.*;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- import flash.net.URLRequest;
- import flash.utils.Timer;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.filters.DropShadowFilter;
- public class As3Sound extends Sprite {
- private var url:String = "http://sxl001.xfyun.com/music/lib/myRussia.mp3";
- private var soundFactory:Sound;
- private var channel:SoundChannel;
- private var positionTimer:Timer;
- private var play_btn:Sprite;
- private var stop_btn:Sprite;
- private var d_filtersropShadowFilter=new DropShadowFilter(5,45,0x000000,80,8,8);
- //用于记录音乐现在是否为暂停状态
- private var bSoundStop:Boolean = false;
- public function As3Sound() {
- var sxl_txt:TextField = new TextField();
- sxl_txt.text="CS4中如何控制声音的播放或停止的";
- sxl_txt.autoSize=TextFieldAutoSize.LEFT;
- sxl_txt.x=stage.stageWidth/2-sxl_txt.width/2;
- sxl_txt.y=20;
- addChild(sxl_txt);
- var mp3_request:URLRequest = new URLRequest(url);
- soundFactory = new Sound();
- //成功加载数据后
- soundFactory.addEventListener(Event.COMPLETE, completeHandler);
- //在存在可用于 MP3 声音的 ID3 数据时
- soundFactory.addEventListener(Event.ID3, id3Handler);
- //加载音乐错误时
- soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
- //音乐加载中...
- soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
- soundFactory.load(mp3_request);
- channel = soundFactory.play();
- //音乐播放完成
- channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
- //用Timer监听音乐的播放进度
- positionTimer = new Timer(1000);
- positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
- positionTimer.start();
- //创建一个按钮,用于播放音乐
- play_btn = new Sprite();
- play_btn.graphics.beginFill(0xFFCC32);
- play_btn.graphics.drawRoundRect(0, 0, 70, 18, 10, 10);
- play_btn.graphics.endFill();
- var play_txt:TextField = new TextField();
- play_txt.text = "播放";
- play_txt.x=18;
- play_btn.x=50;
- play_btn.y=100;
- play_txt.selectable = false;
- play_btn.addChild(play_txt);
- play_btn.filters=[d_filters];
- play_btn.addEventListener(MouseEvent.CLICK, soundPlay);
- addChild(play_btn);
- //创建一个按钮,用于停止音乐
- stop_btn = new Sprite();
- stop_btn.graphics.beginFill(0xFFCC32);
- stop_btn.graphics.drawRoundRect(0, 0, 70, 18, 10, 10);
- stop_btn.graphics.endFill();
- stop_btn.x=130;
- stop_btn.y=100;
- var stop_txt:TextField = new TextField();
- stop_txt.x=18;
- stop_txt.text = "暂停";
- stop_txt.selectable = false;
- stop_btn.addChild(stop_txt);
- stop_btn.filters=[d_filters];
- stop_btn.addEventListener(MouseEvent.CLICK, soundStop);
- addChild(stop_btn);
- }
- //监听音乐的播放进度
- private function positionTimerHandler(event:TimerEvent):void {
- var ybf:int = channel.position.toFixed(0);
- var zcd:int = soundFactory.length;
- var bfs:int = Math.floor(ybf/zcd*100);
- //trace("音乐总长度:"+zcd, "音乐已播放:"+ybf, "播放进度为:"+bfs+"%");
- }
- //加载音乐完成时
- private function completeHandler(event:Event):void {
- //trace("加载音乐完成: " + event);
- }
- //在存在可用于MP3声音的ID3数据时
- private function id3Handler(event:Event):void {
- //trace("音乐的ID3信息如下:");
- for (var s in soundFactory.id3) {
- //trace("\t", s, ":", soundFactory.id3[s]);
- }
- //trace("关于ID3信息介绍,请参见Sound类-->属性-->id3");
- }
- //CuPlayer.com提示:加载音乐错误时
- private function ioErrorHandler(event:Event):void {
- //trace("加载音乐错误,错误信息如下:" + event);
- positionTimer.stop();
- }
- //加载音乐时
- private function progressHandler(eventrogressEvent):void {
- var yjz:int = event.bytesLoaded;
- var zcd:int = event.bytesTotal;
- var bfs:int = Math.floor(yjz/zcd*100);
- //trace("音乐总长度:"+zcd,"已加载: "+yjz, "加载进度为:"+bfs+"%");
- }
- //CuPlayer.com提示:音乐播放完成
- private function soundCompleteHandler(event:Event):void {
- //trace("音乐播放完成: " + event);
- positionTimer.stop();
- }
- //CuPlayer.com提示:点击播放按钮事件
- private function soundPlay(event:MouseEvent):void {
- if (bSoundStop) {
- bSoundStop = false;
- channel = soundFactory.play(channel.position.toFixed(0));
- }
- }
- //CuPlayer.com提示:点击停止按钮事件
- private function soundStop(event:MouseEvent):void {
- if (!bSoundStop) {
- bSoundStop = true;
- channel.stop();
- }
- }
- }
- }
热门文章推荐
- 纯HLS(m3u8)跨平台技术(HLSPlayer,m3u8Player跨平台多终端)
- DiscuzX3.2酷播视频插件(dz论坛自定义视频插件带广告
- [微信视频]实现网站中的视频在微信平台上正常播放(超多组图)
- [rtsp]海康威视监控摄像头实现web端无插件监控实拍效果
- 很酷,酷播wordpress视频插件(支持PC/安卓/苹果跨平台播放)
- [组图]微信视频技术:支持微信视频直播和视频点播
- [AS3]as3.0的rtmp流媒体播放器写法源代码示例
- 一步一步教你制作FLV网页视频播放器
请稍候...