目标:实现声音模块(播放声音和音效)
laya商业级3d游戏开发
方法原形
Laya.SoundManager.playSound(资源地址);
导入素材LayaIde\sound到laya\bin\res\目录下
注意:考虑到多平台兼容性,声音和音效建议都使用MP3格式
新建
scripts\manager\SoundMgr.ts
\方便调用的数据结构
//方便调用的数据结构
export class AudioClip {
url: string;
preTime = 0;
minRate = 0;
public constructor(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没有实现播放广告视频后停止背景音乐,提审将会被驳回
export default 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();
}
}
}
首页添加声音控制按钮
类型imgage
name Btnsound
新建
UIcomponent/Btn/Soundbtn.ts
export default class SoundBtn extends 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 runtime 挂上脚本
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
添加
public static 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 浏览器要鼠标先点击界面才有声音
解决微信切后台后没有声音
新建plaftform/WxMgr.ts
import { EventMgr } from “…/JFrameWork/EventMgr”;
import { EventType } from “…/JFrameWork/EventType”;
export class 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文件到platform目录
补齐
GameSample {
public static commonData:CommonData=new CommonData();
初始化平台信息
Main.ts
onConfigLoaded()增加
Platform.setplatformInfo();