audioManager.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { _decorator, AudioClip, AudioSource, Component, Node, tween } from 'cc';
  2. import { ResourceUtil } from './ResourceUtil';
  3. import { config } from '../config';
  4. import { GameManager } from '../GameManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('audioManager')
  7. export class audioManager extends Component {
  8. public arrSound: any = [];
  9. public audios_map: Map<string,Node> = new Map;
  10. public musicVolume: number = 0.8;
  11. public soundVolume: number = 1;
  12. private static instance:audioManager =null;
  13. public static Instance(){
  14. return this.instance
  15. }
  16. protected start(): void {
  17. audioManager.instance = this
  18. this.init()
  19. }
  20. protected onLoad(): void {
  21. let setting_data = GameManager.getSettingData()
  22. if(setting_data.isOpenYinYue==false) { return }
  23. this.scheduleOnce(()=>{
  24. this.playMusic(config.AUDIO.home_bgm)
  25. },3)
  26. }
  27. public init(){
  28. this.audios_map.clear()
  29. for (const key in config.AUDIO) {
  30. if (Object.prototype.hasOwnProperty.call( config.AUDIO, key)) {
  31. const path = config.AUDIO[key];
  32. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
  33. if(!err){
  34. let node = new Node
  35. node.parent = this.node
  36. node.addComponent(AudioSource).clip = clip
  37. this.audios_map.set(path,node)
  38. }
  39. });
  40. }
  41. }
  42. }
  43. public stopAllBGM(){
  44. if(this.audios_map.get(config.AUDIO.game_bgm)!=null){
  45. this.audios_map.get(config.AUDIO.game_bgm).getComponent(AudioSource).stop()
  46. }
  47. if(this.audios_map.get(config.AUDIO.home_bgm)!=null){
  48. this.audios_map.get(config.AUDIO.home_bgm).getComponent(AudioSource).stop()
  49. }
  50. }
  51. public playBGM(clip_node:Node){
  52. this.stopAllBGM()
  53. clip_node.getComponent(AudioSource).play()
  54. }
  55. public playMusic(path:string){
  56. if(this.audios_map.get(path)!=null){
  57. this.playBGM(this.audios_map.get(path))
  58. }else{
  59. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
  60. let node = new Node
  61. node.parent = this.node
  62. node.addComponent(AudioSource).clip = clip
  63. this.audios_map.set(path,node)
  64. this.playBGM(node)
  65. });
  66. }
  67. }
  68. public playSound(path:string,soundVolume:number = null){
  69. if(!GameManager.getSettingData().isOpenYinXiao) {return}
  70. if(path.length<=0){
  71. return
  72. }
  73. let loop:boolean = false
  74. let play = (node)=>{
  75. node.getComponent(AudioSource).stop()
  76. node.getComponent(AudioSource).play()
  77. // node.getComponent(AudioSource).playOneShot(node.getComponent(AudioSource).clip,soundVolume?soundVolume:this.soundVolume)
  78. }
  79. if(this.audios_map.get(path)){
  80. play(this.audios_map.get(path))
  81. }
  82. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: AudioClip)=> {
  83. let node = new Node
  84. node.parent = this.node
  85. node.addComponent(AudioSource).clip = clip
  86. this.audios_map.set(path,node)
  87. play(node)
  88. });
  89. }
  90. getSound(){
  91. if(this.node.getComponent(AudioSource)!=null){
  92. return this.node.getComponent(AudioSource)
  93. }
  94. return this.node.addComponent(AudioSource)
  95. }
  96. public playHomeBgm() {
  97. if(this.getSound().clip==null){
  98. this.playMusic(config.AUDIO.home_bgm)
  99. } else {
  100. this.getSound().loop = true;
  101. this.getSound().play()
  102. }
  103. }
  104. public pauseHomeBgm() {
  105. this.getSound().stop()
  106. }
  107. }