| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /**
- * 音频播放 mixin
- * 负责:白噪音初始化、播放/暂停、切换音源
- */
- let innerAudioContext = null
- 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
- }
- },
- 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
- })
- },
- 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')
- },
- changeAudio(url, inx) {
- this.curAudioInx = inx
- if (innerAudioContext) {
- innerAudioContext.stop()
- setTimeout(() => {
- innerAudioContext.src = url
- this.ispause = true
- this.playAudio()
- }, 500)
- }
- },
- playAudio() {
- if (!innerAudioContext) return
- if (this.ispause) {
- innerAudioContext.play()
- } else {
- innerAudioContext.pause()
- }
- this.ispause = !this.ispause
- },
- /** 供 ble-mixin 模式切换时调用 */
- _stopAudioOnModeChange() {
- if (innerAudioContext) {
- innerAudioContext.stop()
- }
- },
- destroyAudio() {
- if (innerAudioContext) {
- innerAudioContext.stop()
- innerAudioContext.destroy()
- innerAudioContext = null
- }
- }
- }
- }
|