audioManager.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 playBGM(clip:AudioClip){
  44. if(this.getSound().clip!=null){
  45. this.getSound().stop()
  46. this.getSound().clip = null;
  47. }
  48. this.getSound().clip = clip;
  49. this.getSound().loop = true;
  50. this.getSound().play()
  51. }
  52. public playMusic(path:string){
  53. if(this.audios_map.get(path)!=null){
  54. this.playBGM(this.audios_map.get(path).getComponent(AudioSource).clip)
  55. }else{
  56. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
  57. let node = new Node
  58. node.parent = this.node
  59. node.addComponent(AudioSource).clip = clip
  60. this.audios_map.set(path,node)
  61. this.playBGM(node.addComponent(AudioSource).clip)
  62. });
  63. }
  64. }
  65. public playSound(path:string,soundVolume:number = null){
  66. if(!GameManager.getSettingData().isOpenYinXiao) {return}
  67. if(path.length<=0){
  68. return
  69. }
  70. let loop:boolean = false
  71. let play = (node)=>{
  72. node.getComponent(AudioSource).play()
  73. // node.getComponent(AudioSource).playOneShot(node.getComponent(AudioSource).clip,soundVolume?soundVolume:this.soundVolume)
  74. }
  75. if(this.audios_map.get(path)){
  76. play(this.audios_map.get(path))
  77. }
  78. ResourceUtil.loadRes(path, AudioClip, (err: any, clip: AudioClip)=> {
  79. let node = new Node
  80. node.parent = this.node
  81. node.addComponent(AudioSource).clip = clip
  82. this.audios_map.set(path,node)
  83. play(node)
  84. });
  85. }
  86. getSound(){
  87. if(this.node.getComponent(AudioSource)!=null){
  88. return this.node.getComponent(AudioSource)
  89. }
  90. return this.node.addComponent(AudioSource)
  91. }
  92. public playHomeBgm() {
  93. if(this.getSound().clip==null){
  94. this.playMusic(config.AUDIO.home_bgm)
  95. } else {
  96. this.getSound().loop = true;
  97. this.getSound().play()
  98. }
  99. }
  100. public pauseHomeBgm() {
  101. this.getSound().stop()
  102. }
  103. }