|
@@ -1,22 +1,68 @@
|
|
|
<template>
|
|
|
<div class="ui-audio" @click="clickHandler">
|
|
|
- <audio loop @play="rotation" ref="audio">
|
|
|
+ <!-- <audio :muted="muted" loop @play="rotation" ref="audio">
|
|
|
<source :src="src" />
|
|
|
- </audio>
|
|
|
+ </audio> -->
|
|
|
<span v-for="random in randoms" :style="{ '--percent': random }" />
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
|
<script setup>
|
|
|
-import { defineProps, ref, watchEffect, defineExpose } from 'vue'
|
|
|
-defineProps({
|
|
|
+import { defineProps, ref, watchEffect, defineExpose, defineEmits, onMounted, reactive, onUnmounted, nextTick } from 'vue'
|
|
|
+const props = defineProps({
|
|
|
src: String,
|
|
|
+ muted: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false,
|
|
|
+ },
|
|
|
+})
|
|
|
+const emits = defineEmits(['loaded'])
|
|
|
+let timeout
|
|
|
+let isPause = false
|
|
|
+let isPlay = false
|
|
|
+let soundBgm = new Howl({
|
|
|
+ src: [props.src],
|
|
|
+ loop: true,
|
|
|
+ preload: true,
|
|
|
+ // html5: true,
|
|
|
+ html5: false,
|
|
|
+ format: ['mp3', 'wav'],
|
|
|
+ onplay: () => {
|
|
|
+ isPlay = true
|
|
|
+ },
|
|
|
+ onload: () => {
|
|
|
+ emits('loaded')
|
|
|
+ if (detectWeixin()) {
|
|
|
+ // console.log('detectWeixin')
|
|
|
+ if (window.WeixinJSBridge) {
|
|
|
+ window.WeixinJSBridge &&
|
|
|
+ window.WeixinJSBridge.invoke(
|
|
|
+ 'getNetworkType',
|
|
|
+ {},
|
|
|
+ () => {
|
|
|
+ if (playIng.value && !isPlay && !isPause) {
|
|
|
+ soundBgm.play()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ false
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ if (playIng.value && !isPlay && !isPause) {
|
|
|
+ soundBgm.play()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (playIng.value && !isPlay && !isPause) {
|
|
|
+ soundBgm.play()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
})
|
|
|
-const audio = ref()
|
|
|
+
|
|
|
+const audio = ref(null)
|
|
|
const randoms = ref([1, 0.5, 1, 0.5])
|
|
|
const playIng = ref(false)
|
|
|
|
|
|
-let timeout
|
|
|
const rotation = () => {
|
|
|
if (!playIng.value) return
|
|
|
for (let i = 0; i < randoms.value.length; i++) {
|
|
@@ -26,13 +72,18 @@ const rotation = () => {
|
|
|
}
|
|
|
|
|
|
watchEffect(() => {
|
|
|
- if (audio.value) {
|
|
|
+ // if (audio.value) {
|
|
|
+ if (soundBgm) {
|
|
|
if (playIng.value) {
|
|
|
- audio.value.play()
|
|
|
+ if (soundBgm._state == 'loaded') {
|
|
|
+ soundBgm.play()
|
|
|
+ }
|
|
|
} else {
|
|
|
- audio.value.pause()
|
|
|
+ // audio.value.pause()
|
|
|
+ soundBgm.pause()
|
|
|
}
|
|
|
clearTimeout(timeout)
|
|
|
+ timeout = null
|
|
|
rotation()
|
|
|
}
|
|
|
})
|
|
@@ -47,8 +98,77 @@ defineExpose({
|
|
|
},
|
|
|
pause() {
|
|
|
playIng.value = false
|
|
|
+ isPause = true
|
|
|
},
|
|
|
})
|
|
|
+// 播放音乐
|
|
|
+const audioAutoPlay = () => {
|
|
|
+ soundBgm.on('load', () => {
|
|
|
+ if (window.WeixinJSBridge) {
|
|
|
+ window.WeixinJSBridge &&
|
|
|
+ window.WeixinJSBridge.invoke(
|
|
|
+ 'getNetworkType',
|
|
|
+ {},
|
|
|
+ () => {
|
|
|
+ if (playIng.value && !isPlay && !isPause) {
|
|
|
+ soundBgm.play()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ false
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ soundBgm.play()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // if (window.WeixinJSBridge) {
|
|
|
+ // WeixinJSBridge.invoke(
|
|
|
+ // 'getNetworkType',
|
|
|
+ // {},
|
|
|
+ // function (e) {
|
|
|
+ // if (audio.value) {
|
|
|
+ // audio.value.play()
|
|
|
+ // }
|
|
|
+ // },
|
|
|
+ // false
|
|
|
+ // )
|
|
|
+ // } else {
|
|
|
+ // document.addEventListener(
|
|
|
+ // 'touchstart',
|
|
|
+ // function () {
|
|
|
+ // WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
|
|
|
+ // if (audio.value) {
|
|
|
+ // audio.value.play()
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // },
|
|
|
+ // false
|
|
|
+ // )
|
|
|
+ // }
|
|
|
+}
|
|
|
+const detectWeixin = () => {
|
|
|
+ //微信 包括PC的微信
|
|
|
+ return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'
|
|
|
+}
|
|
|
+
|
|
|
+onMounted(() => {
|
|
|
+ // console.error('onMounted')
|
|
|
+ // if (detectWeixin()) {
|
|
|
+ // console.log('detectWeixin')
|
|
|
+ // audioAutoPlay()
|
|
|
+ // }
|
|
|
+})
|
|
|
+onUnmounted(() => {
|
|
|
+ // console.error('onUnmounted')
|
|
|
+ soundBgm.pause()
|
|
|
+ soundBgm.off()
|
|
|
+ soundBgm.unload()
|
|
|
+ soundBgm = null
|
|
|
+ if (timeout) {
|
|
|
+ clearTimeout(timeout)
|
|
|
+ timeout = null
|
|
|
+ }
|
|
|
+})
|
|
|
</script>
|
|
|
|
|
|
<script>
|