From 52ade88840fbe6290320e8d2604e7dd80db4405f Mon Sep 17 00:00:00 2001 From: ydy0615 Date: Sun, 12 Apr 2026 11:42:22 +0800 Subject: [PATCH] modified: src/components/TTSPlayer.vue --- src/components/TTSPlayer.vue | 116 ++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 42 deletions(-) diff --git a/src/components/TTSPlayer.vue b/src/components/TTSPlayer.vue index e157501..a8b2b0f 100644 --- a/src/components/TTSPlayer.vue +++ b/src/components/TTSPlayer.vue @@ -3,28 +3,33 @@
-
- - + - -
+ + + +
{{ currentTimeStr }} @@ -75,7 +80,7 @@
- +
@@ -135,8 +140,11 @@ function togglePlay() { if (!audio) return if (isPlaying.value) { audio.pause() + isPlaying.value = false } else { - audio.play() + audio.play().then(() => { + isPlaying.value = true + }).catch(() => {}) } } @@ -187,7 +195,11 @@ function onLoadedMetadata() { duration.value = audio.duration || (props.durationMs / 1000) audio.volume = volume.value audio.playbackRate = playbackRate.value - audio.play() + audio.play().then(() => { + isPlaying.value = true + }).catch(() => { + isPlaying.value = false + }) } function onEnded() { @@ -195,15 +207,14 @@ function onEnded() { currentTime.value = 0 } -watch(isPlaying, (val) => { - const audio = audioRef.value - if (!audio) return - if (val) { - audio.play().catch(() => { isPlaying.value = false }) - } else { - audio.pause() - } -}) +// 监听音频实际播放状态(用户可能通过其他方式暂停) +function onPlay() { + isPlaying.value = true +} + +function onPause() { + isPlaying.value = false +} watch(volume, (val) => { if (audioRef.value) { @@ -220,7 +231,8 @@ watch(playbackRate, (val) => { watch(() => props.visible, (val) => { if (val) { - isPlaying.value = true + // 打开播放器时重置状态,等待音频加载完成后才真正播放 + isPlaying.value = false currentTime.value = 0 } else { if (audioRef.value) { @@ -245,16 +257,19 @@ onUnmounted(() => {