Egret之Sound壓縮方案

一 : 前景

如果一個遊戲需要一次預加載多個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的播放.如圖:
Egret之Sound壓縮方案

Ⅱ:播放器的實現(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音效

Ⅲ : 交互界面

Egret之Sound壓縮方案
注意 : 每一種音效播放完畢後 , 才能繼續播放音效

三 : 效果

Egret之Sound壓縮方案

四:資源

源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章