AudioMgr.ts 3.3 KB

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