audio.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * 音频播放 Pinia Store
  3. * 跨页面共享音频播放状态,音频实例存放在 globalData 中确保单例
  4. */
  5. import { defineStore } from 'pinia'
  6. const AUDIO_LIST = [
  7. { name: '河流', posterimg: '/static/device/heliu.png', url: '/static/audio/audio1.mp3' },
  8. { name: '下雨', posterimg: '/static/device/heliu.png', url: '/static/audio/audio2.mp3' }
  9. ]
  10. // 通过 globalData 确保 InnerAudioContext 跨页面单例
  11. function _getAudioCtx() {
  12. const app = getApp()
  13. if (!app.globalData) app.globalData = {}
  14. return app.globalData._audioContext || null
  15. }
  16. function _setAudioCtx(ctx) {
  17. const app = getApp()
  18. if (!app.globalData) app.globalData = {}
  19. app.globalData._audioContext = ctx
  20. }
  21. export const useAudioStore = defineStore('audio', {
  22. state: () => ({
  23. audiolist: AUDIO_LIST,
  24. curAudioInx: 0,
  25. totalTime: 0,
  26. currentTime: 0,
  27. ispause: true, // true=暂停中, false=播放中
  28. isShowDrawer3: false
  29. }),
  30. actions: {
  31. /** 初始化或恢复音频上下文(页面进入时调用) */
  32. initAudio() {
  33. let ctx = _getAudioCtx()
  34. if (ctx) {
  35. // 已有实例,重新绑定事件监听
  36. this._bindEvents(ctx)
  37. // 同步当前播放状态
  38. if (!ctx.paused && ctx.src) {
  39. this.ispause = false
  40. this.currentTime = ctx.currentTime || 0
  41. this.totalTime = ctx.duration || 0
  42. }
  43. return
  44. }
  45. // 首次创建
  46. ctx = uni.createInnerAudioContext()
  47. ctx.src = this.audiolist[this.curAudioInx].url
  48. ctx.loop = true
  49. _setAudioCtx(ctx)
  50. this._bindEvents(ctx)
  51. },
  52. /** 绑定事件监听 */
  53. _bindEvents(ctx) {
  54. // 先清除旧监听,避免重复绑定(部分平台新建ctx调用off会报错,需保护)
  55. try {
  56. ctx.offPlay && ctx.offPlay()
  57. ctx.offCanplay && ctx.offCanplay()
  58. ctx.offError && ctx.offError()
  59. ctx.offTimeUpdate && ctx.offTimeUpdate()
  60. ctx.offPause && ctx.offPause()
  61. ctx.offStop && ctx.offStop()
  62. } catch (e) {
  63. // 忽略:新创建的ctx可能没有已注册的监听器
  64. }
  65. ctx.onPlay(() => {
  66. this.totalTime = ctx.duration || 0
  67. this.ispause = false
  68. })
  69. ctx.onCanplay(() => {
  70. this.totalTime = ctx.duration || 0
  71. })
  72. ctx.onError((res) => {
  73. console.log('播放错误', res.errMsg)
  74. })
  75. ctx.onTimeUpdate(() => {
  76. this.currentTime = ctx.currentTime || 0
  77. })
  78. ctx.onPause(() => {
  79. this.ispause = true
  80. })
  81. ctx.onStop(() => {
  82. this.ispause = true
  83. this.currentTime = 0
  84. })
  85. },
  86. /** 页面离开时解绑事件(不销毁音频) */
  87. detachEvents() {
  88. const ctx = _getAudioCtx()
  89. if (ctx) {
  90. try {
  91. ctx.offPlay && ctx.offPlay()
  92. ctx.offCanplay && ctx.offCanplay()
  93. ctx.offError && ctx.offError()
  94. ctx.offTimeUpdate && ctx.offTimeUpdate()
  95. ctx.offPause && ctx.offPause()
  96. ctx.offStop && ctx.offStop()
  97. } catch (e) {
  98. // 忽略
  99. }
  100. }
  101. },
  102. /** 切换播放/暂停 */
  103. playAudio() {
  104. const ctx = _getAudioCtx()
  105. if (!ctx) return
  106. if (this.ispause) {
  107. ctx.play()
  108. } else {
  109. ctx.pause()
  110. }
  111. },
  112. /** 切换音源(切换后主动播放) */
  113. changeAudio(url, inx) {
  114. this.curAudioInx = inx
  115. this.ispause = false
  116. const ctx = _getAudioCtx()
  117. if (ctx) {
  118. ctx.stop()
  119. setTimeout(() => {
  120. ctx.src = url
  121. ctx.play()
  122. }, 300)
  123. } else {
  124. // 音频上下文不存在时,先初始化再播放
  125. this.initAudio()
  126. const newCtx = _getAudioCtx()
  127. if (newCtx) {
  128. newCtx.src = url
  129. newCtx.play()
  130. }
  131. }
  132. },
  133. /** 停止播放(模式切换时调用) */
  134. stopAudio() {
  135. const ctx = _getAudioCtx()
  136. if (ctx) {
  137. ctx.stop()
  138. }
  139. this.ispause = true
  140. this.currentTime = 0
  141. },
  142. /** 彻底销毁音频(APP退出等极端场景) */
  143. destroyAudio() {
  144. const ctx = _getAudioCtx()
  145. if (ctx) {
  146. ctx.stop()
  147. ctx.destroy()
  148. _setAudioCtx(null)
  149. }
  150. this.ispause = true
  151. this.currentTime = 0
  152. this.totalTime = 0
  153. },
  154. /** 格式化时间 */
  155. formatTime(totalSeconds) {
  156. totalSeconds = Math.floor(totalSeconds || 0)
  157. const minutes = Math.floor(totalSeconds / 60)
  158. const seconds = totalSeconds % 60
  159. return minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0')
  160. }
  161. }
  162. })