AudioModel.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Gamecfg from "../../../common/gameCfg"
  2. import { gameMethod } from "../../../common/gameMethod"
  3. import AssetsBundleMgr from "../../../utils/AssetsBundleMgr"
  4. import { AudioConst } from "../../const/TypeConst"
  5. import GameDataCenter from "../../GameDataCenter"
  6. import IDataModel from "../IDataModel"
  7. export class AudioModel extends IDataModel {
  8. bgmID: string
  9. bgmName: string
  10. constructor() {
  11. super('audio')
  12. this.bgmID = ""
  13. this.bgmName = ""
  14. }
  15. getBgmId(): string {
  16. return this.bgmID;
  17. }
  18. playMusic(id: string) {
  19. if (id == AudioConst.empty) { return }
  20. if (!GameDataCenter.setting.setting.musicOn) { return }
  21. if (GameDataCenter.setting.setting.musicVol == 0) { return }
  22. // 安全检查音乐是否正在播放
  23. let isMusicPlaying = false;
  24. try {
  25. if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
  26. isMusicPlaying = cc.audioEngine && typeof cc.audioEngine.isMusicPlaying === 'function' && cc.audioEngine.isMusicPlaying();
  27. }
  28. } catch (e) {
  29. console.log("检查音乐播放状态出错:", e)
  30. }
  31. if (this.bgmID == id && isMusicPlaying) { return }
  32. let cfg = Gamecfg.audioInfo.getItem(id)
  33. if (cfg == null || gameMethod.isEmpty(cfg.name)) { return }
  34. if (this.bgmName == cfg.name) {
  35. return
  36. }
  37. this.stopMusic()
  38. this.bgmID = id
  39. this.bgmName = cfg.name
  40. AssetsBundleMgr.loadBundle("audio", (err, bundle) => {
  41. bundle.load(cfg.name, cc.AudioClip, (error, asset: cc.AudioClip) => {
  42. cc.audioEngine.setMusicVolume(cfg.vol / 100)
  43. cc.audioEngine.playMusic(asset, true)
  44. })
  45. })
  46. }
  47. playEffect(id: string, loop: boolean = false) {
  48. if (id.length == 0) { return }
  49. if (!GameDataCenter.setting.setting.effectOn) { return }
  50. if (GameDataCenter.setting.setting.effectVol == 0) { return }
  51. if (id == AudioConst.empty) { return }
  52. let cfg = Gamecfg.audioInfo.getItem(id)
  53. if (cfg == null || gameMethod.isEmpty(cfg.name)) { return }
  54. AssetsBundleMgr.loadBundle("audio", (err, bundle) => {
  55. bundle.load(cfg.name, cc.AudioClip, (error, asset: cc.AudioClip) => {
  56. if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
  57. // 非淘宝小游戏,淘宝小游戏不支持设置音量
  58. cc.audioEngine.setEffectsVolume(cfg.vol / 100)
  59. }
  60. cc.audioEngine.playEffect(asset, loop)
  61. })
  62. })
  63. }
  64. // 关闭音乐
  65. stopMusic() {
  66. // 记录播放时间点
  67. // if (this.lastBgm) {
  68. // this.currentTimeList[this.lastBgm.name] = cc.audioEngine.getCurrentTime(this.lastBgm.id)
  69. // }
  70. try {
  71. if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
  72. if (cc.audioEngine && typeof cc.audioEngine.isMusicPlaying === 'function' && cc.audioEngine.isMusicPlaying()) {
  73. cc.audioEngine.stopMusic()
  74. }
  75. } else {
  76. // 淘宝小游戏平台直接调用停止,不做播放检查
  77. cc.audioEngine.stopMusic()
  78. }
  79. } catch (e) {
  80. console.log("停止音乐出错:", e)
  81. }
  82. }
  83. // 关掉音效
  84. stopAllEffects() {
  85. try {
  86. cc.audioEngine.stopAllEffects()
  87. } catch (e) {
  88. console.log("停止音效出错:", e)
  89. }
  90. }
  91. // 暂停音乐
  92. pauseMusic() {
  93. try {
  94. if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
  95. if (cc.audioEngine && typeof cc.audioEngine.isMusicPlaying === 'function' && cc.audioEngine.isMusicPlaying()) {
  96. cc.audioEngine.pauseMusic()
  97. }
  98. } else {
  99. // 淘宝小游戏平台直接调用暂停,不做播放检查
  100. cc.audioEngine.pauseMusic()
  101. }
  102. } catch (e) {
  103. console.log("暂停音乐出错:", e)
  104. }
  105. }
  106. // 继续音乐
  107. resumeMusic() {
  108. try {
  109. cc.audioEngine.resumeMusic()
  110. } catch (e) {
  111. console.log("继续播放音乐出错:", e)
  112. }
  113. }
  114. }