audioManager.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if(GameManager.getSettingData().isOpenYinYue==false) { return }
  22. this.scheduleOnce(()=>{
  23. this.playMusic(config.AUDIO.home_bgm)
  24. },3)
  25. }
  26. public init(){
  27. this.audios_map.clear()
  28. for (const key in config.AUDIO) {
  29. if (Object.prototype.hasOwnProperty.call( config.AUDIO, key)) {
  30. const path = config.AUDIO[key];
  31. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
  32. if(!err){
  33. let node = new Node
  34. node.parent = this.node
  35. node.addComponent(AudioSource).clip = clip
  36. this.audios_map.set(path,node)
  37. }
  38. });
  39. }
  40. }
  41. }
  42. public stopAllBGM(){
  43. if(this.audios_map.get(config.AUDIO.game_bgm)!=null){
  44. this.audios_map.get(config.AUDIO.game_bgm).getComponent(AudioSource).stop()
  45. }
  46. if(this.audios_map.get(config.AUDIO.home_bgm)!=null){
  47. this.audios_map.get(config.AUDIO.home_bgm).getComponent(AudioSource).stop()
  48. }
  49. }
  50. public playBGM(clip_node:Node){
  51. this.stopAllBGM()
  52. clip_node.getComponent(AudioSource).loop = true
  53. clip_node.getComponent(AudioSource).play()
  54. }
  55. public playMusic(path:string){
  56. if(!GameManager.getSettingData().isOpenYinYue) {return}
  57. if(this.audios_map.get(path)!=null){
  58. this.playBGM(this.audios_map.get(path))
  59. }else{
  60. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
  61. let node = new Node
  62. node.parent = this.node
  63. node.addComponent(AudioSource).clip = clip
  64. this.audios_map.set(path,node)
  65. this.playBGM(node)
  66. });
  67. }
  68. }
  69. public playSound(path:string,soundVolume:number = null){
  70. if(!GameManager.getSettingData().isOpenYinXiao) {return}
  71. if(path.length<=0){
  72. return
  73. }
  74. let loop:boolean = false
  75. let play = (node:Node)=>{
  76. node.getComponent(AudioSource).stop()
  77. node.getComponent(AudioSource).playOneShot(node.getComponent(AudioSource).clip,1)
  78. // node.getComponent(AudioSource).playOneShot(node.getComponent(AudioSource).clip,soundVolume?soundVolume:this.soundVolume)
  79. }
  80. if(this.audios_map.get(path)){
  81. play(this.audios_map.get(path))
  82. }
  83. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: AudioClip)=> {
  84. let node = new Node
  85. node.parent = this.node
  86. node.addComponent(AudioSource).clip = clip
  87. this.audios_map.set(path,node)
  88. play(node)
  89. });
  90. }
  91. getSound(){
  92. if(this.node.getComponent(AudioSource)!=null){
  93. return this.node.getComponent(AudioSource)
  94. }
  95. return this.node.addComponent(AudioSource)
  96. }
  97. public playHomeBgm() {
  98. if(!GameManager.getSettingData().isOpenYinYue) {return}
  99. if(this.getSound().clip==null){
  100. this.playMusic(config.AUDIO.home_bgm)
  101. } else {
  102. this.getSound().loop = true;
  103. this.getSound().play()
  104. }
  105. }
  106. public pauseHomeBgm() {
  107. // this.getSound().stop()
  108. this.stopAllBGM()
  109. }
  110. }