|
|
@@ -1,84 +1,66 @@
|
|
|
/**
|
|
|
* 音频播放 mixin
|
|
|
- * 负责:白噪音初始化、播放/暂停、切换音源
|
|
|
+ * 代理 audioStore,实现跨页面音频持久化播放
|
|
|
+ * 退出页面后音频继续播放,再次进入页面恢复播放状态
|
|
|
*/
|
|
|
-let innerAudioContext = null
|
|
|
+import { useAudioStore } from '@/stores/audio'
|
|
|
|
|
|
export default {
|
|
|
- data() {
|
|
|
- return {
|
|
|
- audiolist: [
|
|
|
- { name: '河流', posterimg: '/static/device/heliu.png', url: '/static/audio/audio1.mp3' },
|
|
|
- { name: '下雨', posterimg: '/static/device/heliu.png', url: '/static/audio/audio2.mp3' }
|
|
|
- ],
|
|
|
- curAudioInx: 0,
|
|
|
- totalTime: 0,
|
|
|
- currentTime: 0,
|
|
|
- ispause: true,
|
|
|
- isShowDrawer3: false
|
|
|
+ computed: {
|
|
|
+ _audioStore() {
|
|
|
+ return useAudioStore()
|
|
|
+ },
|
|
|
+ audiolist() {
|
|
|
+ return this._audioStore.audiolist
|
|
|
+ },
|
|
|
+ curAudioInx: {
|
|
|
+ get() { return this._audioStore.curAudioInx },
|
|
|
+ set(val) { this._audioStore.curAudioInx = val }
|
|
|
+ },
|
|
|
+ totalTime() {
|
|
|
+ return this._audioStore.totalTime
|
|
|
+ },
|
|
|
+ currentTime() {
|
|
|
+ return this._audioStore.currentTime
|
|
|
+ },
|
|
|
+ ispause() {
|
|
|
+ return this._audioStore.ispause
|
|
|
+ },
|
|
|
+ isShowDrawer3: {
|
|
|
+ get() { return this._audioStore.isShowDrawer3 },
|
|
|
+ set(val) { this._audioStore.isShowDrawer3 = val }
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
initAudio() {
|
|
|
- innerAudioContext = uni.createInnerAudioContext()
|
|
|
- innerAudioContext.src = '/static/audio/audio1.mp3'
|
|
|
- innerAudioContext.loop = true
|
|
|
- innerAudioContext.onPlay(() => {
|
|
|
- this.totalTime = innerAudioContext.duration
|
|
|
- })
|
|
|
- innerAudioContext.onCanplay(() => {
|
|
|
- this.totalTime = innerAudioContext.duration
|
|
|
- })
|
|
|
- innerAudioContext.onError((res) => {
|
|
|
- console.log('播放错误', res.errMsg)
|
|
|
- })
|
|
|
- innerAudioContext.onTimeUpdate(() => {
|
|
|
- this.currentTime = innerAudioContext.currentTime
|
|
|
- })
|
|
|
+ this._audioStore.initAudio()
|
|
|
},
|
|
|
|
|
|
formatTime(totalSeconds) {
|
|
|
- totalSeconds = Math.floor(totalSeconds)
|
|
|
- const minutes = Math.floor(totalSeconds / 60)
|
|
|
- const seconds = totalSeconds % 60
|
|
|
- return minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0')
|
|
|
+ return this._audioStore.formatTime(totalSeconds)
|
|
|
},
|
|
|
|
|
|
changeAudio(url, inx) {
|
|
|
- this.curAudioInx = inx
|
|
|
- if (innerAudioContext) {
|
|
|
- innerAudioContext.stop()
|
|
|
- setTimeout(() => {
|
|
|
- innerAudioContext.src = url
|
|
|
- this.ispause = true
|
|
|
- this.playAudio()
|
|
|
- }, 500)
|
|
|
- }
|
|
|
+ this._audioStore.changeAudio(url, inx)
|
|
|
},
|
|
|
|
|
|
playAudio() {
|
|
|
- if (!innerAudioContext) return
|
|
|
- if (this.ispause) {
|
|
|
- innerAudioContext.play()
|
|
|
- } else {
|
|
|
- innerAudioContext.pause()
|
|
|
- }
|
|
|
- this.ispause = !this.ispause
|
|
|
+ this._audioStore.playAudio()
|
|
|
},
|
|
|
|
|
|
/** 供 ble-mixin 模式切换时调用 */
|
|
|
_stopAudioOnModeChange() {
|
|
|
- if (innerAudioContext) {
|
|
|
- innerAudioContext.stop()
|
|
|
- }
|
|
|
+ this._audioStore.stopAudio()
|
|
|
+ },
|
|
|
+
|
|
|
+ /** 页面离开时解绑事件(音频继续播放) */
|
|
|
+ detachAudio() {
|
|
|
+ this._audioStore.detachEvents()
|
|
|
},
|
|
|
|
|
|
+ /** 彻底销毁(一般不调用,仅供极端场景) */
|
|
|
destroyAudio() {
|
|
|
- if (innerAudioContext) {
|
|
|
- innerAudioContext.stop()
|
|
|
- innerAudioContext.destroy()
|
|
|
- innerAudioContext = null
|
|
|
- }
|
|
|
+ this._audioStore.destroyAudio()
|
|
|
}
|
|
|
}
|
|
|
}
|