音频播放bug修复
This commit is contained in:
@@ -1,129 +1,133 @@
|
||||
import { _decorator, Node, AudioClip, AudioSource, director, assetManager } from "cc";
|
||||
import {
|
||||
_decorator,
|
||||
Node,
|
||||
AudioClip,
|
||||
AudioSource,
|
||||
director,
|
||||
assetManager,
|
||||
} from "cc";
|
||||
import PlayerDataManager from "./PlayerDataManager";
|
||||
import ResManager from "./ResManager";
|
||||
import Resource from "../Config/Resource";
|
||||
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
||||
import { Config18x } from "../Config/Config18x";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
//音频管理器
|
||||
@ccclass('AudioManager')
|
||||
@ccclass("AudioManager")
|
||||
export default class AudioManager {
|
||||
private _audoSource: AudioSource;
|
||||
private curBgId: string; //当前播放中的背景乐id 用于记录
|
||||
private lastBgId: string; //上次播放的背景乐id 用于音乐开关切换时播放
|
||||
private curBgId_confirm: string; //当前播放中的背景乐id 实际播放
|
||||
|
||||
private _audoSource: AudioSource;
|
||||
private curBgId: number = -1; //当前播放中的背景乐id 用于记录
|
||||
private lastBgId: number = -1; //上次播放的背景乐id 用于音乐开关切换时播放
|
||||
private curBgId_confirm: number = -1; //当前播放中的背景乐id 实际播放
|
||||
private static _I: AudioManager = null;
|
||||
public static get I(): AudioManager {
|
||||
if (!AudioManager._I) {
|
||||
AudioManager._I = new AudioManager();
|
||||
AudioManager._I.init();
|
||||
}
|
||||
return AudioManager._I;
|
||||
}
|
||||
|
||||
private init() {
|
||||
let audioMgr = new Node();
|
||||
audioMgr.name = "__audioMgr__";
|
||||
director.getScene().addChild(audioMgr);
|
||||
director.addPersistRootNode(audioMgr);
|
||||
this._audoSource = audioMgr.addComponent(AudioSource);
|
||||
}
|
||||
|
||||
private static _I: AudioManager = null;
|
||||
public static get I(): AudioManager {
|
||||
if (!AudioManager._I) {
|
||||
AudioManager._I = new AudioManager();
|
||||
AudioManager._I.init();
|
||||
/**播放音效 */
|
||||
PlayEffect(sound: string, volume: number = 1.0) {
|
||||
if (!sound) return;
|
||||
if (!PlayerDataManager.I.IsSoundOn) {
|
||||
return;
|
||||
}
|
||||
|
||||
//let cfgs =
|
||||
//if (!cfgs[sound]) return;
|
||||
|
||||
//let path = `Sound/${cfgs[sound].AssetName}`
|
||||
logger.log("PlayEffect:", sound);
|
||||
ResManager.I.getAudio(sound, (res: AudioClip) => {
|
||||
this._audoSource.playOneShot(res, volume);
|
||||
});
|
||||
}
|
||||
|
||||
/**播放背景乐 */
|
||||
PlayMusic(sound: string, loop: boolean = true, volume: number = 1.0) {
|
||||
if (!sound || sound == this.curBgId_confirm) return;
|
||||
|
||||
//let cfgs = Resource.getConfig("Music")
|
||||
//if (!cfgs[sound]) return;
|
||||
|
||||
this.lastBgId = sound;
|
||||
|
||||
if (!PlayerDataManager.I.IsMusicOn) {
|
||||
return; //这个放在记录后面判断,防止开关打开时,没有记录
|
||||
}
|
||||
|
||||
this.StopMusic();
|
||||
this.curBgId = sound;
|
||||
this.curBgId_confirm = sound;
|
||||
//let path = `Music/${cfgs[sound].AssetName}`
|
||||
logger.log("PlayMusic:", sound);
|
||||
ResManager.I.getAudio(sound, (res: AudioClip) => {
|
||||
this._audoSource.clip = res;
|
||||
this._audoSource.play();
|
||||
this._audoSource.volume = volume;
|
||||
this._audoSource.loop = loop;
|
||||
});
|
||||
}
|
||||
|
||||
/**播放远程背景乐 */
|
||||
PlayRemoteMusic(url, loop: boolean = true, volume: number = 1.0) {
|
||||
logger.log(`下载远程背景乐: url= ${url}`);
|
||||
assetManager.loadRemote<AudioClip>(
|
||||
url,
|
||||
{ ext: ".mp3" },
|
||||
(err, res: AudioClip) => {
|
||||
if (err) {
|
||||
logger.log(err);
|
||||
return;
|
||||
}
|
||||
return AudioManager._I;
|
||||
logger.log(`播放远程背景乐: res= ${res}`);
|
||||
this.StopMusic();
|
||||
this._audoSource.clip = res;
|
||||
this._audoSource.play();
|
||||
this._audoSource.volume = volume;
|
||||
this._audoSource.loop = loop;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**停止播放背景乐 */
|
||||
StopMusic() {
|
||||
if (this.curBgId != null) {
|
||||
this.lastBgId = this.curBgId;
|
||||
}
|
||||
this._audoSource.stop();
|
||||
this.curBgId = null;
|
||||
this.curBgId_confirm = null;
|
||||
}
|
||||
|
||||
private init(){
|
||||
let audioMgr = new Node();
|
||||
audioMgr.name = '__audioMgr__';
|
||||
director.getScene().addChild(audioMgr);
|
||||
director.addPersistRootNode(audioMgr);
|
||||
this._audoSource = audioMgr.addComponent(AudioSource);
|
||||
}
|
||||
// 重新播放背景乐
|
||||
ReplayMusic(): boolean {
|
||||
if (this.curBgId != null) return false;
|
||||
if (this.lastBgId == null) return false;
|
||||
|
||||
|
||||
/**播放音效 */
|
||||
PlayEffect(sound, volume:number=1.0) {
|
||||
if (!sound) return;
|
||||
if (!PlayerDataManager.I.IsSoundOn) {
|
||||
return;
|
||||
}
|
||||
|
||||
let cfgs = Resource.getConfig("Sound")
|
||||
if (!cfgs[sound]) return;
|
||||
this.PlayMusic(this.lastBgId);
|
||||
return true;
|
||||
}
|
||||
|
||||
let path = `Sound/${cfgs[sound].AssetName}`
|
||||
logger.log("PlayEffect:", sound, path)
|
||||
ResManager.I.getBundleAudio(path, (res: AudioClip)=>{
|
||||
this._audoSource.playOneShot(res, volume)
|
||||
});
|
||||
}
|
||||
/**暂停播放背景乐 */
|
||||
PauseMusic() {
|
||||
this._audoSource.pause();
|
||||
}
|
||||
|
||||
|
||||
/**播放背景乐 */
|
||||
PlayMusic(sound, loop:boolean = true, volume:number=1.0) {
|
||||
if (!sound || sound == this.curBgId_confirm) return;
|
||||
|
||||
let cfgs = Resource.getConfig("Music")
|
||||
if (!cfgs[sound]) return;
|
||||
|
||||
this.lastBgId = sound;
|
||||
|
||||
if (!PlayerDataManager.I.IsMusicOn) {
|
||||
return; //这个放在记录后面判断,防止开关打开时,没有记录
|
||||
}
|
||||
|
||||
this.StopMusic()
|
||||
this.curBgId = sound;
|
||||
this.curBgId_confirm = sound;
|
||||
let path = `Music/${cfgs[sound].AssetName}`
|
||||
logger.log("PlayMusic:", sound, path)
|
||||
ResManager.I.getBundleAudio(path, (res: AudioClip)=>{
|
||||
this._audoSource.clip = res
|
||||
this._audoSource.play()
|
||||
this._audoSource.volume = volume
|
||||
this._audoSource.loop = loop
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**播放远程背景乐 */
|
||||
PlayRemoteMusic(url, loop:boolean = true, volume:number=1.0) {
|
||||
logger.log(`下载远程背景乐: url= ${url}`)
|
||||
assetManager.loadRemote<AudioClip>(url, { ext: '.mp3'}, (err, res: AudioClip) => {
|
||||
if (err) {
|
||||
logger.log(err);
|
||||
return
|
||||
}
|
||||
logger.log(`播放远程背景乐: res= ${res}`)
|
||||
this.StopMusic()
|
||||
this._audoSource.clip = res
|
||||
this._audoSource.play()
|
||||
this._audoSource.volume = volume
|
||||
this._audoSource.loop = loop
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**停止播放背景乐 */
|
||||
StopMusic(){
|
||||
this._audoSource.stop()
|
||||
this.curBgId = -1
|
||||
this.curBgId_confirm = -1
|
||||
}
|
||||
|
||||
// 重新播放背景乐
|
||||
ReplayMusic(){
|
||||
if (this.curBgId > -1)
|
||||
return
|
||||
if (this.lastBgId < 0)
|
||||
return
|
||||
|
||||
this.PlayMusic(this.lastBgId)
|
||||
}
|
||||
|
||||
/**暂停播放背景乐 */
|
||||
PauseMusic(){
|
||||
this._audoSource.pause()
|
||||
}
|
||||
|
||||
|
||||
/**继续播放背景乐 */
|
||||
ResumeMusic(){
|
||||
this._audoSource.play()
|
||||
}
|
||||
|
||||
}
|
||||
/**继续播放背景乐 */
|
||||
ResumeMusic() {
|
||||
this._audoSource.play();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { _decorator, Node, AudioClip, AudioSource, director, PhysicsSystem2D, EPhysics2DDrawFlags, Vec2, PHYSICS_2D_PTM_RATIO, v2 } from "cc";
|
||||
import PlayerDataManager from "./PlayerDataManager";
|
||||
import ResManager from "./ResManager";
|
||||
import Resource from "../Config/Resource";
|
||||
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
|
||||
@@ -4,127 +4,140 @@ import { CommonConfig } from "../Config/CommonConfig";
|
||||
import AudioManager from "./AudioManager";
|
||||
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('PlayerDataManager')
|
||||
@ccclass("PlayerDataManager")
|
||||
export default class PlayerDataManager {
|
||||
private static _I: PlayerDataManager = null;
|
||||
public static get I(): PlayerDataManager {
|
||||
if (!PlayerDataManager._I) {
|
||||
PlayerDataManager._I = new PlayerDataManager();
|
||||
PlayerDataManager._I.init()
|
||||
}
|
||||
return PlayerDataManager._I;
|
||||
private static _I: PlayerDataManager = null;
|
||||
public static get I(): PlayerDataManager {
|
||||
if (!PlayerDataManager._I) {
|
||||
PlayerDataManager._I = new PlayerDataManager();
|
||||
PlayerDataManager._I.init();
|
||||
}
|
||||
return PlayerDataManager._I;
|
||||
}
|
||||
|
||||
private isShakeOn: boolean = true; //震动
|
||||
private isMusicOn: boolean = true; //音乐
|
||||
private isSoundOn: boolean = true; //音效
|
||||
private guideMainFinished: boolean = false; //主界面引导是否完成
|
||||
private guideTalkFinished: boolean = false; //聊天界面引导是否完成
|
||||
private isShakeOn: boolean = true; //震动
|
||||
private isMusicOn: boolean = true; //音乐
|
||||
private isSoundOn: boolean = true; //音效
|
||||
private guideMainFinished: boolean = false; //主界面引导是否完成
|
||||
private guideTalkFinished: boolean = false; //聊天界面引导是否完成
|
||||
|
||||
public get IsShakeOn(): boolean {
|
||||
return this.isShakeOn;
|
||||
}
|
||||
public get IsMusicOn(): boolean {
|
||||
return this.isMusicOn;
|
||||
}
|
||||
public get IsSoundOn(): boolean {
|
||||
return this.isSoundOn;
|
||||
}
|
||||
public get GuideMainFinished(): boolean {
|
||||
return this.guideMainFinished;
|
||||
}
|
||||
public get GuideTalkFinished(): boolean {
|
||||
return this.guideTalkFinished;
|
||||
}
|
||||
|
||||
public get IsShakeOn() : boolean {return this.isShakeOn;}
|
||||
public get IsMusicOn() : boolean {return this.isMusicOn;}
|
||||
public get IsSoundOn() : boolean {return this.isSoundOn;}
|
||||
public get GuideMainFinished() : boolean {return this.guideMainFinished;}
|
||||
public get GuideTalkFinished() : boolean {return this.guideTalkFinished;}
|
||||
private init() {
|
||||
this.InitPlayerData();
|
||||
}
|
||||
public initEmpty() {}
|
||||
|
||||
private InitPlayerData() {
|
||||
// this.isShakeOn = PlayerDataManager.getForKey(CommonConfig.StorageConfig.SETTING_SHAKEON, true)
|
||||
this.isMusicOn = PlayerDataManager.getForKey(
|
||||
CommonConfig.StorageConfig.SETTING_MUSIC,
|
||||
true
|
||||
);
|
||||
this.isSoundOn = PlayerDataManager.getForKey(
|
||||
CommonConfig.StorageConfig.SETTING_SOUND,
|
||||
true
|
||||
);
|
||||
this.guideMainFinished = PlayerDataManager.getForKey(
|
||||
CommonConfig.StorageConfig.GUIDE_MAIN,
|
||||
false
|
||||
);
|
||||
this.guideTalkFinished = PlayerDataManager.getForKey(
|
||||
CommonConfig.StorageConfig.GUIDE_TALK,
|
||||
false
|
||||
);
|
||||
logger.log(
|
||||
"InitPlayerData",
|
||||
`music=${this.isMusicOn}, sound=${this.isSoundOn}, guideMain=${this.guideMainFinished}, guideTalk=${this.guideTalkFinished}`
|
||||
);
|
||||
}
|
||||
|
||||
private init(){
|
||||
this.InitPlayerData();
|
||||
}
|
||||
public initEmpty(){
|
||||
|
||||
public SetMusicOn(bo: boolean) {
|
||||
if (this.isMusicOn == bo) return;
|
||||
this.isMusicOn = bo;
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.SETTING_MUSIC, bo);
|
||||
if (bo) {
|
||||
AudioManager.I.ReplayMusic();
|
||||
} else AudioManager.I.StopMusic();
|
||||
}
|
||||
|
||||
public SetSoundOn(bo: boolean) {
|
||||
if (this.isSoundOn == bo) return;
|
||||
this.isSoundOn = bo;
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.SETTING_SOUND, bo);
|
||||
}
|
||||
|
||||
/**主界面引导完成*/
|
||||
public SetGuideMainFinish(bo: boolean) {
|
||||
if (this.guideMainFinished == bo) return;
|
||||
this.guideMainFinished = bo;
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.GUIDE_MAIN, bo);
|
||||
}
|
||||
/**聊天界面引导完成*/
|
||||
public SetGuideTalkFinish(bo: boolean) {
|
||||
if (this.guideTalkFinished == bo) return;
|
||||
this.guideTalkFinished = bo;
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.GUIDE_TALK, bo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 类型内部处理,外层部分类型 isBindingId //标识位数据需要绑定 playerid的时候,默认值为 true,若是传 false,则不进入绑定
|
||||
* */
|
||||
static setForKey(key: number | string, data) {
|
||||
if (!key || void 0 === data) {
|
||||
Utils.Log("key can`t nil");
|
||||
return;
|
||||
}
|
||||
key = key + "";
|
||||
sys.localStorage.setItem(key, JSON.stringify(data));
|
||||
}
|
||||
|
||||
|
||||
private InitPlayerData() {
|
||||
// this.isShakeOn = PlayerDataManager.getForKey(CommonConfig.StorageConfig.SETTING_SHAKEON, true)
|
||||
this.isMusicOn = PlayerDataManager.getForKey(CommonConfig.StorageConfig.SETTING_MUSIC, true)
|
||||
this.isSoundOn = PlayerDataManager.getForKey(CommonConfig.StorageConfig.SETTING_SOUND, true)
|
||||
this.guideMainFinished = PlayerDataManager.getForKey(CommonConfig.StorageConfig.GUIDE_MAIN, false)
|
||||
this.guideTalkFinished = PlayerDataManager.getForKey(CommonConfig.StorageConfig.GUIDE_TALK, false)
|
||||
logger.log("InitPlayerData", `music=${this.isMusicOn}, sound=${this.isSoundOn}, guideMain=${this.guideMainFinished}, guideTalk=${this.guideTalkFinished}`)
|
||||
static getForKey(key: number | string, defaultVal): any {
|
||||
if (!key) {
|
||||
Utils.Log("key can`t nil");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
public SetMusicOn(bo: boolean) {
|
||||
if (this.isMusicOn == bo)
|
||||
return
|
||||
this.isMusicOn = bo
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.SETTING_MUSIC, bo)
|
||||
if (bo)
|
||||
AudioManager.I.ReplayMusic()
|
||||
else
|
||||
AudioManager.I.StopMusic()
|
||||
key = key + "";
|
||||
let jsonObj;
|
||||
try {
|
||||
let str = sys.localStorage.getItem(key);
|
||||
try {
|
||||
jsonObj = JSON.parse(str);
|
||||
} catch (error) {
|
||||
jsonObj = str;
|
||||
}
|
||||
} catch (error) {
|
||||
Utils.Log(`getForKey--->>>解析${key}的值的时候报错`);
|
||||
}
|
||||
if (jsonObj == null) {
|
||||
jsonObj = defaultVal;
|
||||
}
|
||||
return jsonObj;
|
||||
}
|
||||
|
||||
public SetSoundOn(bo: boolean) {
|
||||
if (this.isSoundOn == bo)
|
||||
return
|
||||
this.isSoundOn = bo
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.SETTING_SOUND, bo)
|
||||
}
|
||||
static removeForKey(key: number | string): void {
|
||||
key = key + "";
|
||||
sys.localStorage.setItem(key, "");
|
||||
}
|
||||
|
||||
/**主界面引导完成*/
|
||||
public SetGuideMainFinish(bo: boolean) {
|
||||
if (this.guideMainFinished == bo)
|
||||
return
|
||||
this.guideMainFinished = bo
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.GUIDE_MAIN, bo)
|
||||
}
|
||||
/**聊天界面引导完成*/
|
||||
public SetGuideTalkFinish(bo: boolean) {
|
||||
if (this.guideTalkFinished == bo)
|
||||
return
|
||||
this.guideTalkFinished = bo
|
||||
PlayerDataManager.setForKey(CommonConfig.StorageConfig.GUIDE_TALK, bo)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 类型内部处理,外层部分类型 isBindingId //标识位数据需要绑定 playerid的时候,默认值为 true,若是传 false,则不进入绑定
|
||||
* */
|
||||
static setForKey(key: number | string, data) {
|
||||
if (!key || void (0) === data) {
|
||||
Utils.Log("key can`t nil");
|
||||
return;
|
||||
}
|
||||
key = key + "";
|
||||
sys.localStorage.setItem(key, JSON.stringify(data));
|
||||
}
|
||||
|
||||
static getForKey(key: number | string, defaultVal): any {
|
||||
if (!key) {
|
||||
Utils.Log("key can`t nil");
|
||||
return;
|
||||
}
|
||||
key = key + "";
|
||||
let jsonObj;
|
||||
try {
|
||||
let str = sys.localStorage.getItem(key);
|
||||
try {
|
||||
jsonObj = JSON.parse(str);
|
||||
} catch (error) {
|
||||
jsonObj = str;
|
||||
}
|
||||
} catch (error) {
|
||||
Utils.Log(`getForKey--->>>解析${key}的值的时候报错`);
|
||||
}
|
||||
if (jsonObj == null){
|
||||
jsonObj = defaultVal;
|
||||
}
|
||||
return jsonObj;
|
||||
}
|
||||
|
||||
static removeForKey(key: number | string): void {
|
||||
key = key + "";
|
||||
sys.localStorage.setItem(key, "");
|
||||
}
|
||||
|
||||
/**清档 */
|
||||
static clearAll(): void {
|
||||
sys.localStorage.clear();
|
||||
}
|
||||
}
|
||||
/**清档 */
|
||||
static clearAll(): void {
|
||||
sys.localStorage.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,28 +307,25 @@ export default class ResManager {
|
||||
}
|
||||
|
||||
/** 加载bundle中的音频文件*/
|
||||
getBundleAudio(url: string, cb) {
|
||||
let bundle = assetManager.getBundle("Audio");
|
||||
if (bundle) {
|
||||
bundle.load(url, AudioClip, (err, res: AudioClip) => {
|
||||
if (err) {
|
||||
logger.log(err);
|
||||
return;
|
||||
}
|
||||
cb && cb(res);
|
||||
});
|
||||
} else {
|
||||
assetManager.loadBundle(
|
||||
"Audio",
|
||||
(err: Error, _bundle: AssetManager.Bundle) => {
|
||||
if (err) {
|
||||
logger.log(err);
|
||||
return;
|
||||
}
|
||||
this.getBundleAudio(url, cb);
|
||||
}
|
||||
);
|
||||
}
|
||||
getAudio(url: string, cb) {
|
||||
//let bundle = assetManager.getBundle("Audio");
|
||||
// if (bundle) {
|
||||
// bundle.load(url, AudioClip, (err, res: AudioClip) => {
|
||||
// if (err) {
|
||||
// logger.log(err);
|
||||
// return;
|
||||
// }
|
||||
// cb && cb(res);
|
||||
// });
|
||||
// } else {
|
||||
resources.load(url, AudioClip, (err, res: AudioClip) => {
|
||||
if (err) {
|
||||
logger.log(err);
|
||||
return;
|
||||
}
|
||||
cb && cb(res);
|
||||
});
|
||||
//}
|
||||
}
|
||||
|
||||
/** 加载bundle中的配置表*/
|
||||
|
||||
Reference in New Issue
Block a user