AudioManager.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { Component, game, Node } from "cc";
  2. import { storage } from "../storage/SqlUtil";
  3. import { AudioEffect } from "./AudioEffect";
  4. import { AudioMusic } from "./AudioMusic";
  5. export interface AudioConf {
  6. bundle?: string; //BundleName
  7. url: string; //资源路径
  8. }
  9. export class AudioManager extends Component {
  10. private _audio_conf: { [key: number]: AudioConf } = {};
  11. private _local_data: any = {};
  12. private _music: AudioMusic = null;
  13. private _effect: AudioEffect = null;
  14. private _volume_music: number = 1;
  15. private _volume_effect: number = 1;
  16. private _switch_music: boolean = true;
  17. private _switch_effect: boolean = true;
  18. private _load_key = "audio_conf";
  19. private static _instance: AudioManager;
  20. static get instance() {
  21. if (!this._instance) {
  22. let node = new Node("Audio_Manager");
  23. game.addPersistRootNode(node);
  24. this._instance = node.addComponent(AudioManager);
  25. this._instance._init();
  26. let music = new Node("Music_Node");
  27. node.addChild(music);
  28. this._instance._music = music.addComponent(AudioMusic);
  29. let effect = new Node("Effect_Node");
  30. node.addChild(effect);
  31. this._instance._effect = effect.addComponent(AudioEffect);
  32. }
  33. return this._instance;
  34. }
  35. private _init() {
  36. let data = storage.get(this._load_key);
  37. if (data) {
  38. try {
  39. this._local_data = JSON.parse(data);
  40. this._volume_music = this._local_data.volume_music;
  41. this._volume_effect = this._local_data.volume_effect;
  42. this._switch_music = this._local_data.switch_music;
  43. this._switch_effect = this._local_data.switch_effect;
  44. } catch (e) {
  45. this._local_data = {};
  46. this._volume_music = 1;
  47. this._volume_effect = 1;
  48. this._switch_music = true;
  49. this._switch_effect = true;
  50. }
  51. this._music.volume = this._volume_music;
  52. this._effect.volume = this._volume_effect;
  53. }
  54. }
  55. init(conf: any) {
  56. this._audio_conf = conf;
  57. }
  58. /**
  59. * 播放背景音乐
  60. * @param id 资源ID
  61. * @param callback 音乐播放完成事件
  62. */
  63. playMusic(id: number, callback: Function = null,loop: boolean = true) {
  64. let conf = this._audio_conf[id];
  65. if (conf && this._switch_music) {
  66. if(!this._music){
  67. this._music=new AudioMusic()
  68. }
  69. this._music.load(conf.bundle, conf.url,null,loop);
  70. this._music.onComplete = callback;
  71. }
  72. }
  73. /**
  74. * 播放音效
  75. * @param id 资源ID
  76. */
  77. playEffect(id: number) {
  78. let conf = this._audio_conf[id];
  79. if (conf && this._switch_effect) {
  80. if(!this._effect){
  81. this._effect=new AudioEffect()
  82. }
  83. this._effect.load(conf.bundle, conf.url);
  84. }
  85. }
  86. //背景音乐音量
  87. get musicVolume() {
  88. return this._volume_music;
  89. }
  90. set musicVolume(value: number) {
  91. this._volume_music = value;
  92. this._music.volume = value;
  93. }
  94. //音效音量
  95. get effectVolume() {
  96. return this._volume_effect;
  97. }
  98. set effectVolume(value: number) {
  99. this._volume_effect = value;
  100. this._effect.volume = value;
  101. }
  102. //音乐开关
  103. getSwitchMusic() {
  104. return this._switch_music;
  105. }
  106. setSwitchMusic(value: boolean) {
  107. this._switch_music = value;
  108. (!value) && this.stopMusic();
  109. }
  110. //音效开关
  111. getSwitchEffect() {
  112. return this._switch_effect;
  113. }
  114. setSwitchEffect(value: boolean) {
  115. this._switch_effect = value;
  116. (!value) && this._effect.stop();
  117. }
  118. resumeMusic() {
  119. (this._music) && this._music.play();
  120. }
  121. pauseMusic() {
  122. (this._music) && this._music.pause();
  123. }
  124. stopMusic() {
  125. (this._music) && this._music.stop();
  126. }
  127. save() {
  128. this._local_data.volume_music = this._volume_music;
  129. this._local_data.volume_effect = this._volume_effect;
  130. this._local_data.switch_music = this._switch_music;
  131. this._local_data.switch_effect = this._switch_effect;
  132. let data = JSON.stringify(this._local_data);
  133. storage.set(this._load_key, data);
  134. }
  135. }