AudioMng.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { _decorator,AudioSource,AudioClip } from 'cc';
  2. import { GameMng } from '../GameMng';
  3. import { userData } from '../UserData/userData';
  4. import GameData from './GameData';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('AudioMng')
  7. export default class AudioMng {
  8. private static _audioSource?: AudioSource;
  9. private audioSource: AudioSource | null = null;
  10. private bgmClip: AudioClip | null = null;
  11. private static _instance: AudioMng | null = null;
  12. public static get Instance() {
  13. if (AudioMng._instance === null){
  14. // var s = JSON.stringify({"id":"123123123","name":"xiaoming","room":{"time":"3.1415926"}})
  15. // var obj = JSON.parse(s)
  16. // var d = obj as userData
  17. // console.log("id == ",d.room)
  18. AudioMng._instance = new AudioMng();
  19. }
  20. return AudioMng._instance;
  21. }
  22. public InitSaveData() {
  23. GameData.SetCustomData("BgmOn", 1);
  24. GameData.SetCustomData("SoundOn", 1);
  25. GameData.SetCustomData("Volume", 1);
  26. }
  27. public get bgmOn() {
  28. return GameData.GetCustomData("BgmOn") == 1;
  29. }
  30. public get soundOn() {
  31. return GameData.GetCustomData("SoundOn") == 1;
  32. }
  33. public get volume() {
  34. return GameData.GetCustomData("Volume");
  35. }
  36. public PlayBGM(bgm:AudioClip) {
  37. if (this.audioSource == null)
  38. this.audioSource = new AudioSource();
  39. // if (!this.bgmOn) return;
  40. // this.StopBGM();
  41. // this.bgmClip = null;
  42. // this.bgmClip = bgm;
  43. // this.audioSource.clip = this.bgmClip;
  44. // this.audioSource.loop = true;
  45. // this.audioSource.volume = this.volume;
  46. // this.audioSource.play();
  47. // console.log(this.bgmClip)
  48. }
  49. public StopBGM(): void {
  50. if (this.audioSource) {
  51. // this.audioSource.stop();
  52. }
  53. }
  54. public PauseBGM(): void {
  55. if (this.audioSource) {
  56. // this.audioSource.pause();
  57. }
  58. }
  59. /**
  60. * 音效
  61. * @param name
  62. */
  63. public PlaySoundByName(clip: AudioClip) {
  64. if (!this.soundOn) return;
  65. // var eid = audioEngine.playEffect(clip, false);
  66. // audioEngine.setVolume(eid, 1);
  67. if(clip&&this.audioSource){
  68. this.audioSource.playOneShot(clip,1)
  69. }
  70. }
  71. public SetVolume(num: number): void {
  72. if (this.audioSource) {
  73. this.audioSource.volume = num;
  74. GameData.SetCustomData("Volume", num);
  75. }
  76. }
  77. }