HTML5游戏制作之路_07_egret的声音播放的三种方式
2015-11-19 22:47
811 查看
/*
我目前使用的版本是egret2.5
*/
egret支持的声音格式是mp3.获取资源的方式和之前的一样。
1. 使用URLLoader方式加载
json格式
我目前使用的版本是egret2.5
*/
egret支持的声音格式是mp3.获取资源的方式和之前的一样。
1. 使用URLLoader方式加载
/** * 这是使用URL方法加载。 * 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 loader:egret.URLLoader; private loadSound():void{ //新建一个URLLoader this.loader= new egret.URLLoader(); this.loader.addEventListener(egret.Event.COMPLETE,this.soundPlayComplete,this); //新建一个数据表单 this.loader.dataFormat = egret.URLLoaderDataFormat.SOUND; //将声音添加进去 this.loader.load(new egret.URLRequest("resource/assets/Hai.mp3")) } private soundPlayComplete(event:egret.Event){ //取得表单数据 var sound:egret.Sound = this.loader.data; sound.play(); //alert("声音播放完毕"); } }2.使用RES方式加载
json格式
/** * 这是使用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 loadSoundComplete(event:RES.ResourceEvent):void{ var sound:egret.Sound = RES.getRes("mp0"); sound.play(); } }3.直接播放
var sound:egret.Sound = new egret.Sound(); sound.addEventListener(egret.Event.COMPLETE, function loadOver(event:egret.Event) { sound.play(); }, this); sound.addEventListener(egret.IOErrorEvent.IO_ERROR, function loadError(event:egret.IOErrorEvent) { console.log("loaded error!"); }, this); sound.load("resource/sound/sound.mp3");
相关文章推荐
- 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>元素
- HTML5<nav>元素
- HTML5应用程序缓存
- HTML5<header>元素
- HTML5语义