audioManager.ts 3.9 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).loop = true
  54. clip_node.getComponent(AudioSource).play()
  55. }
  56. public playMusic(path:string){
  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(this.getSound().clip==null){
  99. this.playMusic(config.AUDIO.home_bgm)
  100. } else {
  101. this.getSound().loop = true;
  102. this.getSound().play()
  103. }
  104. }
  105. public pauseHomeBgm() {
  106. this.getSound().stop()
  107. }
  108. }