AudioManager.ts 3.7 KB

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