Skip to content

Commit

Permalink
fix:音频播放器在标签切换的时候报错的问题 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
chunhuili12138 authored Oct 9, 2022
1 parent 1f65fe2 commit c11f496
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/components/HbAdminAudio.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup>
import { useMessage, NSlider, useThemeVars } from 'naive-ui'
import { onMounted, ref } from 'vue'
import {nextTick, onBeforeUnmount, onMounted, ref} from 'vue'
const themeVars = useThemeVars()
const message = useMessage()
Expand All @@ -25,8 +25,11 @@ const nowDuration = ref(0)
const progress = ref(0)
function handleSlider (val) {
handlePause()
progress.value = val
hbAudio.value.currentTime = duration.value * val / 100
nextTick(() => {
hbAudio.value.currentTime = duration.value * val / 100
})
}
function formatTooltip (val) {
Expand All @@ -39,12 +42,7 @@ function initialAudio () {
nowDuration.value = 0
hbAudio.value.oncanplay = () => {
duration.value = Math.ceil(hbAudio.value.duration)
hbAudio.value.addEventListener('timeupdate', () => {
nowDuration.value = Math.ceil(hbAudio.value.currentTime)
if (duration.value > 0) {
progress.value = Math.floor(nowDuration.value / duration.value * 100)
}
})
hbAudio.value.addEventListener('timeupdate', timeupdateListener)
hbAudio.value.addEventListener('ended', () => {
if (!loop.value) {
state.value = false
Expand All @@ -56,6 +54,13 @@ function initialAudio () {
}
}
function timeupdateListener () {
nowDuration.value = Math.ceil(hbAudio.value.currentTime)
if (duration.value > 0) {
progress.value = Math.floor(nowDuration.value / duration.value * 100)
}
}
function formatTime (time = 0) {
if (time > 60) {
const m = Math.floor(time / 60)
Expand Down Expand Up @@ -92,6 +97,10 @@ onMounted(() => {
initialAudio()
})
onBeforeUnmount(() => {
hbAudio.value.removeEventListener('timeupdate', timeupdateListener)
})
function handlePlay () {
hbAudio.value.play()
state.value = true
Expand Down

1 comment on commit c11f496

@vercel
Copy link

@vercel vercel bot commented on c11f496 Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.