| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- // 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) {}
- }
|