目標:サウンドモジュールを実現する(サウンドとサウンドエフェクトを再生する)
ラヤコマーシャル3Dゲーム開発
メソッドプロトタイプ
Laya.SoundManager.playSound(リソースアドレス);
マテリアルLayaIde \ soundをlaya \ bin \ res \ディレクトリにインポートします
注:マルチプラットフォームの互換性を考慮すると、サウンドとサウンドエフェクトの両方にMP3形式を使用することをお勧めします
新しい
scripts \ manager \ SoundMgr.ts
\データ構造の呼び出しが簡単
//便利なデータ構造
エクスポートクラスAudioClip { url:string; preTime = 0; minRate = 0; publicconstructor(p_url:string、pminRate){ this.url = p_url; this.minRate = pminRate;
}
public Play() {
if (Time.time - this.preTime < this.minRate)
return;
SoundMgr.instance.PlayOneShot(this.url);
this.preTime = Time.time;
}
}
//カプセル化機能
//アップルの舞台裏がフォアグラウンドに切り替わり、音楽を再生します。アップルはメニューをプルアップしてサウンドの再生に戻ります。//vivo
が広告ビデオを再生せず、バックグラウンドミュージックを停止した場合、レビューは拒否され
ます。exportdefault class SoundMgr {
private static minstance: SoundMgr
public static get instance() {
if (SoundMgr.minstance == null) SoundMgr.minstance = new SoundMgr();
return SoundMgr.minstance;
}
readonly bgmurl = 'res/sound/main/bgm.mp3';
readonly bgmurl2 = 'res/sound/game/bgm.mp3';
readonly clcik = new AudioClip('res/sound/main/click.mp3', 0);
//最小播放间隔0.5秒,播放频率太高会对性能有影响
readonly FishCollection = new AudioClip('res/sound/game/FishCollection.mp3', 0.5);
readonly CatDeath = new AudioClip('res/sound/game/CatDeath.mp3', 1);
constructor() {
//广告视频关闭后重新播放声音 只有vivo需要
EventMgr.instance.on(EventType.AD_VIDEO_CLOSE_EVENT, this, this.onAD_VIDEO_CLOSE_EVENT);
//微信onshow时恢复声音
EventMgr.instance.on(EventType.WxOnShow, this, this.ReSumeBgm);
}
static isPlaySound(): boolean {
return PlayerPrefs.GetInt('PlaySound', 1) == 1;
}
static setPlaySound(isPlaySound: boolean) {
PlayerPrefs.SetInt('PlaySound', isPlaySound ? 1 : 0);
}
curbgm_SoundChannel: Laya.SoundChannel;
onAD_VIDEO_CLOSE_EVENT() {
if (Laya.Browser.onVVMiniGame)
this.ReSumeBgm();
}
play(url: string) {
Laya.SoundManager.playSound(url, 1);
}
PlayOneShot(url: string) {
if (SoundMgr.isPlaySound()) {
Laya.SoundManager.playSound(url, 1);
}
}
BgmPlay() {
this.PlayBgm(this.bgmurl, true);
}
PlayGameBgm() {
// this.BgmPlaySwitch();
this.PlayBgm(this.bgmurl2, true);
}
BgmPlaySetting() {
this.PlayBgm(this.bgmurl, true);
}
/**后台从前台恢复声音 */
public ReSumeBgm() {
console.log('ReSumeBgm');
//if (LayaSample.commonData.subpackage_sound_LoadOk == false) return;
// if (LayaSample.commonData.wxUpWake == false) return;
let isPlaySound = SoundMgr.isPlaySound();
if (!isPlaySound) return;
{
// Laya.SoundManager.playMusic(this._pathRoot + "bgm.mp3", 0);
this.curbgm_SoundChannel.resume();
}
}
public pauseBgm() {
if (this.curbgm_SoundChannel != null)
this.curbgm_SoundChannel.pause();
}
prebgmurl = "";
PlayBgm(url: string, loop: boolean) {
if (this.prebgmurl != url)
this.curbgm_SoundChannel = null;
let isPlaySound = SoundMgr.isPlaySound();
console.log('isPlaySound=' + isPlaySound);
if (isPlaySound) {
console.log('PlayBgm');
let num = 1;
if (loop) num = 0;
//使用缓存变量,防止ios上第二次播放音乐不了的问题
if (this.curbgm_SoundChannel == null) {
let channel = Laya.SoundManager.playMusic(url, num);
this.curbgm_SoundChannel = channel;
}
else
this.curbgm_SoundChannel.play();
}
}
}
ホームページにサウンドコントロールボタンを追加するimgagenameBtnsoundと
入力します
新建
UIcomponent / Btn / Soundbtn.tsexport
デフォルトクラスSoundBtnはLaya.Imageを拡張します{
onAwake() {
this.skin = SoundMgr.isPlaySound() ? 'Textrue/btn_sound_on.png' : 'Textrue/btn_sound_off.png';
Utils.addClickEvent(this, this, this.OnClick);
}
OnClick() {
let isplay = !SoundMgr.isPlaySound();
SoundMgr.setPlaySound(isplay);
this.skin = isplay ? 'Textrue/btn_sound_on.png' : 'Textrue/btn_sound_off.png';
if (isplay)
SoundMgr.instance.BgmPlaySetting();
else
SoundMgr.instance.pauseBgm();
}
}
Btnsoundランタイムは、スクリプト
HomeView.ts
onOpened
SoundMgr.instance.BgmPlay();をハングさせます。
viewMgr.ts
public OpenHome(callder、callbackFunc:Function){
Laya.Scene.open(SceneType.Home, false, Laya.Handler.create(callder, view => {
let s = view as HomeView;
if (callbackFunc != null)
callbackFunc.apply(callder);
}));
}
GameSample.ts
添加
パブリック静的GotoHome(){ ViewMgr.instance.OpenHome(NULL、NULL); }
ゲームシーンにサウンドを追加する
Game.ts
OnAwake()
SoundMgr.instance.PlayGameBgm();
playerEatFish() {
SoundMgr.instance.FishCollection.Play();
Player.ts
Fail(){ SoundMgr.instance.CatDeath.Play();
Mian.ts
onConfigLoaded():void { let node = new Laya.Node(); Laya.stage.addChild(node); node.addComponent(UnityEnagine); //ホームページのサウンドをテストするGameSample.GotoHome(); //ゲームをテストするサウンドGameSample.StartGame();
クリックしてサウンドを制御
障害物にぶつかって「ミュー」を作った
注意。。Chromeブラウザでは、音が鳴る前にマウスでインターフェースをクリックする必要があります
WeChatをバックグラウンドに切り替えた後、音が出ないという問題を解決します。新しいplaftform /WxMgr.tsを作成します。import
{EventMgr} from“…/ JFrameWork / EventMgr”;
import {EventType} from“…/ JFrameWork / EventType”;
エクスポートクラスWxmgr {
constructor() {
console.log('wxmgr init')
this._regisiterWXCallback();
}
_regisiterWXCallback() {
let p_window: any = window["wx"]
// p_window.offShow(this.onShowEvent);
p_window.onShow(this.onWxShowEvent);
// p_window.offHide(this.onHideEvent);
// p_window.onHide(this.onHideEvent);
//苹果拉起菜单后播放声音
p_window.onAudioInterruptionEnd(this.onAudioInterruptionEnd)
}
onWxShowEvent(res) {
console.log('wxmgr onWxShowEvent')
EventMgr.instance.event(EventType.WxOnShow);
}
onAudioInterruptionEnd(res) {
console.log('wxmgr onAudioInterruptionEnd')
EventMgr.instance.event(EventType.WxOnShow);
}
}
資料の下のPlatform.tsファイルをプラットフォームディレクトリにコピーします
补齐
GameSample { public static commonData:CommonData = new CommonData();
初期化プラットフォーム情報
Main.ts
onConfigLoaded()増加
)Platform.setplatformInfoを(;