HTML5游戏制作之路_08_egret对于声音的控制
2015-11-20 00:40
1186 查看
/*
egret版本:2.5
*/
一.点击发声
点击播放出了声音。
二.点击发声,点击暂停
测试结果成功。
(注:如果想让声音循环,在sound.play(0,0)。
egret版本:2.5
*/
一.点击发声
/** * 这是使用RES方法加载。 * Created by 13641 on 2015/11/19. */ class SoundControl extends egret.DisplayObjectContainer{ public constructor() { super(); this.addEventListener(egret.Event.ADDED_TO_STAGE, this.mainFunc, this); } private mainFunc():void{ this.loadSound(); } private loadSound():void{ RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.loadSoundComplete,this); RES.loadConfig("resource/default.res.json","resource/"); RES.loadGroup("MPsound"); } private sound:egret.Sound; private loadSoundComplete(event:RES.ResourceEvent):void{ this.sound= RES.getRes("mp0"); this.soundControl(); } private soundControl():void{ var button:egret.Sprite = new egret.Sprite(); button.graphics.beginFill(0x7d7dff); button.graphics.drawRect(100,100,100,100); button.graphics.endFill(); button.width = 100; button.height = 100; this.addChild(button); //重点在这里,点击事件的监听,点击事件的应用,记得要先设置touchEnable = true; button.touchEnabled = true; button.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this); } //点击完成后的点击事件 private onClick(event:egret.Event):void{ this.sound.play(); } }
点击播放出了声音。
二.点击发声,点击暂停
/** * Created by 13641 on 2015/11/20. */ class SoundStartAndStop extends egret.DisplayObjectContainer{ public constructor() { super(); this.addEventListener(egret.Event.ADDED_TO_STAGE, this.mainFunc, this); } private mainFunc():void{ this.loadResource(); } private loadResource():void{ RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.loadComplete,this); RES.loadConfig("resource/default.res.json","resource/"); RES.loadGroup("MPsound"); } private sound:egret.Sound; //注意channel这个变量,协助管理声音控制,很重要。 private channel:egret.SoundChannel; private loadComplete(event:RES.ResourceEvent):void{ this.sound = RES.getRes("mp0"); this.SoundStartControl(); this.CloseStartControl(); } //监听开始按钮的方法 private SoundStartControl():void{ var btn_startSound :egret.Sprite = new egret.Sprite(); btn_startSound.graphics.beginFill(0x00ff00); btn_startSound.graphics.drawCircle(200,200,20); btn_startSound.graphics.endFill(); btn_startSound.width = 200; btn_startSound.height = 200; var lable_start:egret.TextField =new egret.TextField(); lable_start.text = "播放"; lable_start.x = 200; lable_start.y = 200; this.addChild(btn_startSound); this.addChild((lable_start)); btn_startSound.touchEnabled = true; btn_startSound.addEventListener(egret.TouchEvent.TOUCH_TAP,this.startOnClick,this); } //点击开始的回调 private startOnClick(event:egret.TouchEvent):void{ if(this.channel){ this.channel.stop(); } this.channel = this.sound.play(0,1); } //监听停止按钮的方法 private CloseStartControl():void{ var btn_clostSound :egret.Sprite = new egret.Sprite(); btn_clostSound.graphics.beginFill(0x0fff00); btn_clostSound.graphics.drawCircle(100,100,20); btn_clostSound.graphics.endFill(); btn_clostSound.width = 200; btn_clostSound.height = 200; var lable_close:egret.TextField =new egret.TextField(); lable_close.text = "停止"; lable_close.x = 100; lable_close.y = 100; this.addChild(btn_clostSound); this.addChild((lable_close)); btn_clostSound.touchEnabled = true; btn_clostSound.addEventListener(egret.TouchEvent.TOUCH_TAP,this.CloseOnClick,this); } //停止按钮的回调 private CloseOnClick(event:egret.TouchEvent):void{ if(this.channel){ this.channel.stop(); this.channel = null; } } }
测试结果成功。
(注:如果想让声音循环,在sound.play(0,0)。
生成一个新的 SoundChannel 对象来播放该声音。此方法返回 SoundChannel 对象,访问该对象可停止声音调整音量。 * @param startTime 应开始播放的初始位置(以秒为单位),默认值是 0 * @param loops 播放次数,默认值是 0,循环播放。 大于 0 为播放次数,如 1 为播放 1 次;小于等于 0,为循环播放。
相关文章推荐
- HTML5学习笔记
- html5中datalist简单用法
- HTML5 Web Workers
- HTML5游戏制作之路_07_egret的声音播放的三种方式
- H5小动画,分享一下!!
- HTML5游戏制作之路_06_egret文本及相关属性
- HTML5拖放
- HTML5应用程序缓存
- HTML5中表单的创建
- 用HTML5播放IPCamera视频
- HTML5<footer>元素
- 让所有浏览器支持HTML5 video视频标签
- HTML5中Web存储
- base64和图片的互转(HTML5的File实现)
- 探讨H5方向
- 使用HTML5语义标签时要注意的问题
- HTML5<figure>元素
- HTML5<aside>元素
- HTML5<article>元素
- HTML5<section>元素