一 : 前景
如果一個遊戲需要一次預加載多個Sound(比如說10個),傳統的方案那就需要網絡請求10次來加載這些資源.耗費在網絡上的時間成本是很大的.那麼方案來了 : 壓縮Sound!我前段時間糾結于将Sounds壓縮到Zip中,然後解壓成一個一個的Sound來播放.But,随之而來的問題是,我沒有找到方法或者是API能将Zip中的Sound資源還原成egret.Sound對象.其實我挺喜歡這種方案的,奈何能力有限,如有讀者有這方面的方法或者思路請留言或者私信我,不勝感激.最後我找到了另一種方案 : 将所有的Sounds拼接到一個Sound檔案中.如将10個Mp3拼接到1個Mp3中.本文講解此方案的實作:
二 : 實作
Ⅰ: 音效方面
我将飄金币的音效(money.mp3)和踢足球的音效(football.mp3)按順序拼接在了一個mp3檔案中(golds_football.mp3),其中在0.674秒進入到football.mp3的播放.如圖:
Ⅱ:播放器的實作(DemoSound)
module demo{
/**
* 音樂Demo
* <b style="color:red">一個MP3中分段播放多個音效</b>
* <b style="color:green">
* 目的 : 将幾個短的音效拼接成一個mp3檔案,這樣減少網絡請求,可以加快加載進度.尤其是零碎音效比較多的情況
* </b>
* @author Aonaufly
*/
export class DemoSound{
private static _instance : DemoSound = null;
public static get Instance() : DemoSound{
if( !DemoSound._instance )
DemoSound._instance = new DemoSound();
return DemoSound._instance;
}
private _sound : egret.Sound = null;
private _soundChannel : egret.SoundChannel = null;
private _is_playing : boolean = false;
private _interval_id : number = null;
private readonly _sound_start : Array<number> = null;
private _over_position : number = null;
private constructor(){
this._sound_start = [
0,
0.674
];
}
public play( $ty : TYPE_SOUND ) : void{
if( !this._is_playing ){
this._is_playing = true;
if( !this._sound )
this._sound = RES.getRes(`golds_football_mp3`);
let $start_postion : number = null;
switch( $ty ){
case TYPE_SOUND.___MONEY___:
$start_postion = this._sound_start[0];
this._over_position = this._sound_start[1] - 0.001;
break;
case TYPE_SOUND.___FOOTBALL___:
$start_postion = this._sound_start[1];
this._over_position = null;
break;
}
this._soundChannel = this._sound.play( $start_postion,1 );
this.listener2Handler( true );
}
}
private listener2Handler( $isAdd : boolean ) : void{
if( $isAdd ){
this._interval_id = setInterval(
this.onLoopCheck.bind(this),
10
);
if( !this._soundChannel.hasEventListener(egret.Event.SOUND_COMPLETE) )
this._soundChannel.addEventListener( egret.Event.SOUND_COMPLETE , this.onTimeUpdate , this );
}else{
if( this._interval_id && this._interval_id > 0 ){
clearInterval( this._interval_id );
this._interval_id = 0;
}
if( this._soundChannel.hasEventListener(egret.Event.SOUND_COMPLETE) )
this._soundChannel.removeEventListener( egret.Event.SOUND_COMPLETE , this.onTimeUpdate , this );
this._is_playing = false;
}
}
private onTimeUpdate( $e : egret.Event ) : void{
if(!this._over_position){
egret.log(`播放完畢!!!`);
this.sound_over();
}else{
if( this._soundChannel.hasEventListener(egret.Event.SOUND_COMPLETE) )
this._soundChannel.removeEventListener( egret.Event.SOUND_COMPLETE , this.onTimeUpdate , this );
}
}
private onLoopCheck() : void{
if( this._over_position ){
let $position : number = this._soundChannel ? this._soundChannel.position : 0;
egret.log(`sound pos : ${$position}`);
if( $position >= this._over_position ){
this._soundChannel.stop();
this.sound_over();
}
}else{
if( this._interval_id && this._interval_id > 0 ){
clearInterval( this._interval_id );
this._interval_id = 0;
}
}
}
private sound_over() : void{
this.listener2Handler(false);
}
}
export enum TYPE_SOUND{
___MONEY___ = 1,
___FOOTBALL___ = 2
}
}
解釋 :
① :
this._sound_start = [
0,
0.674
];
0 : money音效播放的position(秒) / 0.674 : football音效播放的position
②:
export enum TYPE_SOUND{
MONEY = 1,
FOOTBALL = 2
}
MONEY : 播momey音效 / FOOTBALL : 播football音效