AudioManager.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { assetManager, AudioClip } from "cc";
  2. import { loadType } from "../../ch/audio/audio";
  3. import { ch } from "../../ch/ch";
  4. export class AudioManager {
  5. private get audio() { return ch.audio; }
  6. // 震动
  7. private _switch_vibrate: boolean = true;
  8. /**
  9. * @en
  10. * play short audio, such as strikes,explosions
  11. * @zh
  12. * 播放短音频,比如 打击音效,爆炸音效等
  13. * @param sound clip or url for the audio
  14. * @param interval 同名字音频限制播放间隔(毫秒) (默认:0不限制 特殊系数:>0 <=1 使用音频时间X此系数)
  15. */
  16. constructor(){
  17. const local_data = chsdk.storage.getObject("AudioManager_vibrate");
  18. if (local_data) {
  19. try {
  20. this._switch_vibrate = local_data._switch_vibrate
  21. }
  22. catch (e) {
  23. this._switch_vibrate = true
  24. }
  25. }
  26. else {
  27. this._switch_vibrate = true
  28. }
  29. }
  30. playOneShot(sound: AudioClip | string, interval: number = 0, remote_ext: string = '.mp3') {
  31. this.audio.playOneShot(sound, interval, remote_ext)
  32. }
  33. vibrateShort() {
  34. if (this._switch_vibrate) {
  35. chsdk.vibrateShort()
  36. }
  37. }
  38. vibrateLong() {
  39. if (this._switch_vibrate) {
  40. chsdk.vibrateLong()
  41. }
  42. }
  43. /**
  44. * @en
  45. * play long audio, such as the bg music
  46. * @zh
  47. * 播放长音频,比如 背景音乐
  48. * @param sound clip or url for the sound
  49. */
  50. currentSound = undefined
  51. play(sound: AudioClip | string, remote_ext: string = '.mp3') {
  52. this.currentSound = sound
  53. this.audio.play(sound, remote_ext)
  54. }
  55. /**
  56. * stop the audio play
  57. */
  58. stop() {
  59. this.audio.stop()
  60. }
  61. /**
  62. * pause the audio play
  63. */
  64. pause() {
  65. this.audio.pause()
  66. }
  67. /**
  68. * resume the audio play
  69. */
  70. resume() {
  71. this.audio.resume()
  72. }
  73. /** 重播当前音乐 */
  74. public replay_music(): void {
  75. this.audio.stop()
  76. if (this.currentSound) {
  77. this.play(this.currentSound)
  78. }
  79. // this.audio.replay_music()
  80. }
  81. init(load_type: loadType, bundle_name: string, remote_url: string): void {
  82. this.currentSound = undefined
  83. if (loadType.bundle == load_type) {
  84. assetManager.loadBundle(bundle_name)
  85. }
  86. this.audio.init(load_type, bundle_name, remote_url)
  87. }
  88. /**
  89. * 获取背景音乐开关值
  90. */
  91. get switchMusic(): boolean {
  92. return this.audio.switchMusic;
  93. }
  94. /**
  95. * 设置背景音乐开关值
  96. * @param value 开关值
  97. */
  98. set switchMusic(value: boolean) {
  99. this.audio.switchMusic = value
  100. }
  101. /**
  102. * 获取音效开关值
  103. */
  104. get switchEffect(): boolean {
  105. return this.audio.switchEffect;
  106. }
  107. /**
  108. * 设置音效开关值
  109. * @param value 音效开关值
  110. */
  111. set switchEffect(value: boolean) {
  112. this.audio.switchEffect = value
  113. }
  114. /**
  115. * 获取震动开关值
  116. */
  117. get switch_vibrate(): boolean {
  118. return this._switch_vibrate;
  119. }
  120. /**
  121. * 设置音效开关值
  122. * @param value 音效开关值
  123. */
  124. set switch_vibrate(value: boolean) {
  125. this._switch_vibrate= value
  126. }
  127. save() {
  128. const local_data:any={};
  129. local_data._switch_vibrate = this._switch_vibrate;
  130. chsdk.storage.set("AudioManager_vibrate",local_data);
  131. this.audio.save()
  132. }
  133. /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
  134. load() {
  135. this.audio.load()
  136. }
  137. }
  138. export const audioManager = new AudioManager