sysSound.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { _decorator, assetManager, AudioClip, AudioSource, Component, Node } from 'cc';
  2. import { gameManager } from './gameManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('sysSound')
  5. export class sysSound extends Component {
  6. public playSound(path:string){
  7. if(!gameManager.getUserData().isOpenYinXiao){
  8. return
  9. }
  10. if(path.length<=0){
  11. return
  12. }
  13. let call_back = (err: any, clip: AudioClip)=> {
  14. if(!err){
  15. gameManager.mp3_cache.set(path,clip)
  16. this.playClip(clip);
  17. }
  18. }
  19. if(gameManager.mp3_cache.get(path)){
  20. let clip = gameManager.mp3_cache.get(path);
  21. call_back(null,clip)
  22. }else{
  23. assetManager.loadRemote(path, call_back );
  24. }
  25. }
  26. public playClip (clip: AudioClip) {
  27. this.getSound().stop()
  28. this.getSound().loop = false;
  29. this.getSound().clip = clip;
  30. this.getSound().play()
  31. }
  32. getSound(){
  33. if(this.node.getComponent(AudioSource)!=null){
  34. return this.node.getComponent(AudioSource)
  35. }
  36. return this.node.addComponent(AudioSource)
  37. }
  38. }