AudioMgr.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //AudioManager.ts
  2. import { Node, AudioSource, AudioClip, resources, director, assetManager } from 'cc';
  3. import { BundleName } from '../Config/EnumCfg';
  4. import { User } from './LocalDataMgr';
  5. /**
  6. * @en
  7. * this is a sington class for audio play, can be easily called from anywhere in you project.
  8. * @zh
  9. * 这是一个用于播放音频的单件类,可以很方便地在项目的任何地方调用。
  10. */
  11. export class AudioManager {
  12. private static _instance: AudioManager;
  13. public static get instance(): AudioManager {
  14. if (this._instance == null) {
  15. this._instance = new AudioManager();
  16. }
  17. return this._instance;
  18. }
  19. private _audioSource: AudioSource;
  20. yinxiaoValue: number = 1
  21. yinyueValue: number = 1
  22. constructor() {
  23. //@en create a node as AudioManager
  24. //@zh 创建一个节点作为 AudioManager
  25. let audioMgr = new Node();
  26. audioMgr.name = '__audioMgr__';
  27. //@en add to the scene.
  28. //@zh 添加节点到场景
  29. director.getScene().addChild(audioMgr);
  30. //@en make it as a persistent node, so it won't be destroied when scene change.
  31. //@zh 标记为常驻节点,这样场景切换的时候就不会被销毁了
  32. director.addPersistRootNode(audioMgr);
  33. //@en add AudioSource componrnt to play audios.
  34. //@zh 添加 AudioSource 组件,用于播放音频。
  35. this._audioSource = audioMgr.addComponent(AudioSource);
  36. this.yinxiaoValue = User.userData.sound ? 1 : 0
  37. this.yinyueValue = User.userData.music ? 1 : 0
  38. }
  39. public get audioSource() {
  40. return this._audioSource;
  41. }
  42. /**
  43. * @en
  44. * play short audio, such as strikes,explosions
  45. * @zh
  46. * 播放短音频,比如 打击音效,爆炸音效等
  47. * @param sound clip or url for the audio
  48. * @param volume
  49. */
  50. playOneShot(sound: AudioClip | string, volume: number = 1.0) {
  51. if (sound instanceof AudioClip) {
  52. this._audioSource.playOneShot(sound, volume * this.yinxiaoValue);
  53. }
  54. else {
  55. resources.load(sound, (err, clip: AudioClip) => {
  56. if (err) {
  57. console.log(err);
  58. }
  59. else {
  60. this._audioSource.playOneShot(clip, volume * this.yinxiaoValue);
  61. }
  62. });
  63. }
  64. }
  65. /**
  66. * @en
  67. * play long audio, such as the bg music
  68. * @zh
  69. * 播放长音频,比如 背景音乐
  70. * @param sound clip or url for the sound
  71. * @param volume
  72. */
  73. play(sound: AudioClip | string, volume: number = 1.0) {
  74. if (sound instanceof AudioClip) {
  75. this._audioSource.clip = sound;
  76. this._audioSource.play();
  77. this.audioSource.volume = volume;
  78. }
  79. else {
  80. resources.load(sound, (err, clip: AudioClip) => {
  81. if (err) {
  82. console.log(err);
  83. }
  84. else {
  85. this._audioSource.clip = clip;
  86. this.audioSource.volume = volume;
  87. this._audioSource.play();
  88. }
  89. });
  90. }
  91. }
  92. playBgm(sound: string) {
  93. let bundle = assetManager.getBundle(BundleName.hall)
  94. bundle.load('audio/' + sound, AudioClip, (err, clip) => {
  95. if (err) {
  96. console.error(err)
  97. }
  98. else {
  99. this._audioSource.stop()
  100. this._audioSource.clip = clip;
  101. this._audioSource.loop = true
  102. this._audioSource.play();
  103. this.audioSource.volume = this.yinyueValue;
  104. }
  105. })
  106. }
  107. playBundleAudio(sound: string) {
  108. let bundle = assetManager.getBundle(BundleName.hall)
  109. bundle.load('audio/' + sound, AudioClip, (err, clip) => {
  110. if (err) {
  111. console.error(err)
  112. }
  113. else {
  114. this._audioSource.playOneShot(clip, this.yinxiaoValue)
  115. }
  116. })
  117. }
  118. playOtherBundleAudio(bundlename: string, sound: string) {
  119. let bundle = assetManager.getBundle(bundlename)
  120. bundle.load('audio/' + sound, AudioClip, (err, clip) => {
  121. if (err) {
  122. console.error(err)
  123. }
  124. else {
  125. this._audioSource.playOneShot(clip, this.yinxiaoValue)
  126. }
  127. })
  128. }
  129. /**
  130. * stop the audio play
  131. */
  132. stop() {
  133. this._audioSource.stop();
  134. }
  135. /**
  136. * pause the audio play
  137. */
  138. pause() {
  139. this._audioSource.pause();
  140. }
  141. /**
  142. * resume the audio play
  143. */
  144. resume() {
  145. this._audioSource.play();
  146. }
  147. }