Egret之Sound压缩方案(egret,sound,压缩,移动开发)

时间:2024-04-20 22:23:26 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

一 : 前景

如果一个游戏需要一次预加载多个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 >一个MP3中分段播放多个音效</b> * <b > * 目的 : 将几个短的音效拼接成一个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压缩方案

四:资源

源码

本文:Egret之Sound压缩方案的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:移动端 H5 报表设计准则下一篇:

6 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18