audioManager.ts 3.1 KB

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