// Learn TypeScript: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html import { AUDIO_TYPE } from "./GameM"; const { ccclass, property } = cc._decorator; @ccclass export default class AudioM extends cc.Component { ///单例 private static instance: AudioM = null public static get Instance() { if (this.instance == null) { this.instance = new AudioM() } return this.instance } private _switchMusic = true; // 音乐开关 private _switchEffect = true; // 音效开关 private _effectVolume = 1; // 音效音量 private _musicVolume = 1; // 音乐音量 private _switchTempEffect = true; // 暂时音效开关 // LIFE-CYCLE CALLBACKS: // onLoad () {} start() { } public init() { // 获取本地开关设置 let effect = cc.sys.localStorage.getItem("switchEffect"); if (effect) { if (effect == "true") { this._switchEffect = true; } else { this._switchEffect = false; } } let music = cc.sys.localStorage.getItem("switchMusic"); if (music) { if (music == "true") { this._switchMusic = true; } else { this._switchMusic = false; } } if (this._switchMusic) { this.playMusic(AUDIO_TYPE.bg, true) } } loopEffectId = -1; public playLoopEffect(url) { if (this._switchEffect && this._switchTempEffect) { cc.loader.loadRes('sounds/' + url, (err, cip) => { if (err) { console.error(err); } this.loopEffectId = cc.audioEngine.playEffect(cip, true);//this.bgmVolume }) } } public stopLoopEffect() { if (this.loopEffectId != -1) cc.audioEngine.stopEffect(this.loopEffectId); } /** * 播放音效文件 * url: 音效文件相对地址 * loop: 是否循环播放 */ public playEffect(url, loop = false) { if (this._switchEffect && this._switchTempEffect) { cc.loader.loadRes('sounds/' + url, (err, cip) => { if (err) { console.error(err); } this.sayId = cc.audioEngine.playEffect(cip, false);//this.bgmVolume }) } } private mateId: number = -1; /**播放伙伴音效*/ public playMateEffect(url, loop = false) { this.stopMateEffect(); if (loop) { if (this._switchMusic) { cc.loader.loadRes('sounds/' + url, (err, cip) => { if (err) { console.error(err); } this.mateId = cc.audioEngine.playEffect(cip, loop);//this.bgmVolume }) } } else { if (this._switchEffect && this._switchTempEffect) { cc.loader.loadRes('sounds/' + url, (err, cip) => { if (err) { console.error(err); } this.mateId = cc.audioEngine.playEffect(cip, loop);//this.bgmVolume }) } } } public stopMateEffect() { if (this.mateId != -1) { cc.audioEngine.stopEffect(this.mateId); this.mateId = -1; } } public pauseMateEffect() { if (this.mateId != -1) cc.audioEngine.pauseEffect(this.mateId); } public resumeMateEffect() { if (this.mateId != -1) cc.audioEngine.resumeEffect(this.mateId); } /**判断当前伙伴音效播放状态*/ public IsMatePlayingEffect() { return cc.audioEngine.getState(this.mateId) == cc.audioEngine.AudioState.PLAYING; } private sayId = -1 public playSay(url, loop = false) { if (this.sayId != -1) { this.stopEffect(this.sayId) this.sayId = -1 } this.playEffect(url, loop) } /** 停止播放音效 */ public stopEffect(id) { cc.audioEngine.stopEffect(id); } /** * 转换音效开关 */ public switchEffectFunc() { this._switchEffect = !this._switchEffect; if (!this._switchEffect) { this.setStopAllEffect(); } cc.sys.localStorage.setItem("switchEffect", this._switchEffect.toString()); } /** * 获取音效开关状态 */ public getSwitchEffect() { return this._switchEffect; } /** * 设置音效声音大小 * value: 0.0 - 1.0 */ public setEffectVolume(value) { this._effectVolume = value; cc.audioEngine.setEffectsVolume(value); cc.sys.localStorage.setItem("audio", JSON.stringify({ effect: this._effectVolume, music: this._musicVolume })); } /** * 获取音效大小 * @return 0.0 - 1.0 */ public getEffectVolume() { return cc.audioEngine.getEffectsVolume(); } /** * 恢复当前说暂停的所有音效 */ public setResumeAllEffect() { if (this._switchEffect) { cc.audioEngine.resumeAllEffects(); } } /** * 停止播放所有正在播放的音效 */ public setStopAllEffect() { cc.audioEngine.stopAllEffects(); } /** 暂时开启关闭音效开关 * @param state true 打开 false 关闭 */ public setTempEffect(state) { this._switchTempEffect = state } /** * 背景音乐播放 * url: 资源路径 * loop: 是否循环 */ public playMusic(url, loop = false) { var self = this; cc.loader.loadRes('sounds/' + url, (err, cip) => { if (err) { console.error(err); } cc.audioEngine.playMusic(cip, loop); }) } /** * 转换音乐按钮开关 */ public switchMusicFunc() { this._switchMusic = !this._switchMusic; if (!this._switchMusic) { // this.setPauseMusic(); this.setStopMusic() if (this.IsMatePlayingEffect()) this.pauseMateEffect() } else { // this.setResumeMusic(); if (this.mateId != -1) { this.resumeMateEffect() } else { this.playMusic(AUDIO_TYPE.bg, true) } } cc.sys.localStorage.setItem("switchMusic", this._switchMusic.toString()); } /** * 获取音乐开关状态 */ public getSwitchMusic() { return this._switchMusic; } /** * 暂停当前播放音乐 */ public setPauseMusic() { cc.audioEngine.pauseMusic(); } /** * 恢复当前被暂停音乐音乐 */ public setResumeMusic() { if (this._switchMusic) { cc.audioEngine.resumeMusic(); } } /** * 暂停播放音乐 * releaseData: 控制是否释放音乐资源 */ public setStopMusic() { cc.audioEngine.stopMusic(); } public setMusicVolume(value) { this._musicVolume = value; cc.audioEngine.setMusicVolume(value); cc.sys.localStorage.setItem("audio", JSON.stringify({ effect: this._effectVolume, music: this._musicVolume })); } public getMusicVolume() { return cc.audioEngine.getMusicVolume(); } /** * 音乐是否正在播放(验证些方法来实现背景音乐是否播放完成) * return boolen */ public isMusicPlaying() { return cc.audioEngine.isMusicPlaying(); } /** * 释放指定音效资源 * url */ public releaseAudio(audio: cc.AudioClip) { if (audio) { cc.audioEngine.uncache(audio); } else { cc.error("【音频】资源" + audio + "不存在, 释放失败"); } } public stopAllAudio() { cc.audioEngine.stopAll(); } public releaseAllAudio() { cc.audioEngine.uncacheAll(); } // update (dt) {} }