2019年12月8日

egret基础-音频播放

作者 admin

先定义一个SoundExample的类,继承自egret.DisplayObjectContainer

方法一:

class SoundExample extends egret.DisplayObjectContainer {
public constructor() {
super();
this.once(egret.Event.ADDED_TO_STAGE, this.onAddtoStage, this);
}
private onAddtoStage() {
this.startLoad();
}
private startLoad(): void {
var sound: egret.Sound = new egret.Sound();
sound.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
sound.load("resource/assets/sound/sound_bg.mp3");
}
private onLoadComplete(event: egret.Event): void {
//获取加载到的 Sound 对象
var sound: egret.Sound = event.target;
this.sound = sound;
//一个简单的播放按钮
var btn = new egret.Sprite();
btn.graphics.beginFill(0x18f7ff);
btn.graphics.drawRoundRect(0, 0, 80, 40, 5, 5);
btn.graphics.endFill();
btn.touchEnabled = true;
btn.anchorOffsetX = btn.width / 2;
btn.x = this.stage.stageWidth / 2;
btn.anchorOffsetY = btn.height / 2;
btn.y = this.stage.stageHeight / 2;
//监听按钮的触摸事件
btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouch, this);
this.addChild(btn);
}
private sound: egret.Sound;
private soundChannel: egret.SoundChannel;

private onTouch(event: egret.Event) {
var sound = this.sound;
//获取音频长度
var channel: egret.SoundChannel = this.soundChannel;
if (channel) {
//调用soundChannel对象的stop方法停止播放音频
console.log(channel);
channel.stop();
this.soundChannel = null;
return;
}
//使用SoundChannel播放音频
channel = sound.play(0, -1);
//Egret 3.0.4 新增获取音频长度 length 属性。
console.log(sound.length);
channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);
//保存soundChannel对象
this.soundChannel = channel;
}
private onSoundComplete(event: egret.Event): void {
console.log("onSoundComplete");
}
}

随后在Main.ts里面进行实例化 :

this.soundCurrent = new SoundTest();
this.addChild(this.soundCurrent);

方法二
这里先定义一个SoundTest类,然后也是继承egret.DisplayObjectContainer

class SoundTest extends egret.DisplayObjectContainer {
public constructor() {
super();
}
public loadSoundRES(url) {
var sound: egret.Sound = RES.getRes(url);
sound.play(0, 1);
}
public loadSound(url) {
//创建 Sound 对象
var sound = new egret.Sound();
//添加加载完成侦听
sound.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
//开始加载
sound.load(url);
}
public onLoadComplete(event: egret.Event): void {
//获取加载到的 Sound 对象
var sound: egret.Sound = event.target;
//播放音乐
console.log(sound)
var channel: egret.SoundChannel = sound.play(0, 1);
channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);
}

public onSoundComplete(event: egret.Event): void {
egret.log("onSoundComplete");
}
}

当调用时候 依旧是在Main.ts 里面进行实例化 同时传入想要播放的音频url:
sky.touchEnabled = true;
this.soundCurrent = new SoundTest();
this.addChild(this.soundCurrent);
sky.addEventListener(egret.TouchEvent.TOUCH_TAP, this.playSoundBg, this)
private playSoundBg() {
var url: string = “sound_bg_mp3”;
this.soundCurrent.loadSoundRES(url)
console.log(url)
}