AudioSystem.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * 音频模块
  3. * @author 薛鸿潇
  4. */
  5. export default class AudioSystem {
  6. /**
  7. * 背景音乐播放
  8. * @param clip 音频资源
  9. * @param loop 循环
  10. * @returns audio_id
  11. */
  12. public playMusic(clip: cc.AudioClip, loop: boolean = false): number {
  13. return cc.audioEngine.playMusic(clip, loop);
  14. }
  15. /**
  16. * 播放音效文件
  17. * @param clip
  18. * @param loop
  19. * @returns audio_id
  20. */
  21. public playEffect(clip: cc.AudioClip, loop = false): number {
  22. return cc.audioEngine.playEffect(clip, loop);
  23. }
  24. /**
  25. * 停止播放音效
  26. * @param audio_id
  27. */
  28. public stopEffect(audio_id: number): void {
  29. cc.audioEngine.stopEffect(audio_id);
  30. }
  31. /**
  32. * 设置音效声音大小
  33. * @param value
  34. */
  35. public setEffectVolume(value: number): void {
  36. cc.audioEngine.setEffectsVolume(value);
  37. }
  38. /**
  39. * 获取音效大小
  40. * @return 0.0 - 1.0
  41. */
  42. public getEffectVolume(): number {
  43. return cc.audioEngine.getEffectsVolume();
  44. }
  45. /**
  46. * 停止播放所有正在播放的音效
  47. */
  48. public stopAllEffects() {
  49. cc.audioEngine.stopAllEffects();
  50. }
  51. /**
  52. * 暂停当前播放音乐
  53. */
  54. public pauseMusic() {
  55. cc.audioEngine.pauseMusic();
  56. }
  57. /**
  58. * 恢复当前说暂停的所有音效
  59. */
  60. public resumeAllEffects() {
  61. cc.audioEngine.resumeAllEffects();
  62. }
  63. /**
  64. * 恢复当前被暂停音乐音乐
  65. */
  66. public resumeMusic() {
  67. cc.audioEngine.resumeMusic();
  68. }
  69. /**
  70. * 暂停播放音乐
  71. */
  72. public stopMusic() {
  73. cc.audioEngine.stopMusic();
  74. }
  75. /**
  76. * 设置音乐音量
  77. * @param value 0~1音量
  78. */
  79. public setMusicVolume(value: number) {
  80. cc.audioEngine.setMusicVolume(value);
  81. }
  82. /**
  83. * 获取音乐音量
  84. * @returns {number}
  85. */
  86. public getMusicVolume() {
  87. return cc.audioEngine.getMusicVolume();
  88. }
  89. /**
  90. * 音乐是否正在播放(验证些方法来实现背景音乐是否播放完成)
  91. * @returns {boolean}
  92. */
  93. public isMusicPlaying() {
  94. return cc.audioEngine.isMusicPlaying();
  95. }
  96. /**
  97. * 释放指定音效资源
  98. * @param audio 音频资源
  99. */
  100. public releaseAudio(audio: cc.AudioClip) {
  101. if (!audio) {
  102. cc.error("【音频】资源" + audio + "不存在, 释放失败")
  103. return;
  104. }
  105. cc.audioEngine.uncache(audio);
  106. }
  107. /**
  108. * 停止所有音频
  109. */
  110. public stopAllAudio() {
  111. cc.audioEngine.stopAll();
  112. }
  113. /**
  114. * 释放所有音频
  115. */
  116. public releaseAllAudio() {
  117. cc.audioEngine.uncacheAll();
  118. }
  119. }