您的位置:首页 > Web前端 > HTML5

HTML5游戏制作之路_07_egret的声音播放的三种方式

2015-11-19 22:47 766 查看
/*

我目前使用的版本是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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: