This commit is contained in:
2026-08-14 13:34:21 +08:00
parent 8ba749234e
commit 96348ce39b
53 changed files with 43 additions and 7586 deletions
+4 -1
View File
@@ -13,7 +13,7 @@
<script>
import { personVisual } from '../utils/visual.js'
import { speak, stopSpeak } from '../utils/speech.js'
import { speak, stopSpeak, prefetch } from '../utils/speech.js'
const EMOJI = { normal: '🙂', think: '🤔', happy: '😊', sad: '😢', surprise: '😮' }
const PAUSE_MS = 700 // 句间停顿
@@ -73,6 +73,9 @@ export default {
}
})
this.after(MAX_LINE_MS, () => this.forceNext(i))
// 预载下一句音频:弱网下句间不卡顿
const next = this.script[i + 1]
if (next && next.audio) prefetch(next.audio)
try {
speak(line.text, line.audio, () => {
// 行号守卫:迟到的第 i 句 onEnd 不得提前放行当前句
+15
View File
@@ -35,6 +35,21 @@ export function speak(text, fileUrl, onEnd) {
speakBrowser(text, onEnd)
}
// prefetch 预载下一句音频(句间衔接不卡):H5 用原生 Audio 预载,App/小程序用 innerAudioContext 预载
export function prefetch(url) {
if (!url) return
if (typeof uni !== 'undefined' && uni.createInnerAudioContext) {
const a = uni.createInnerAudioContext()
a.src = url
a.stop()
setTimeout(() => a.destroy(), 60000)
} else if (typeof window !== 'undefined' && window.Audio) {
const a = new Audio()
a.preload = 'auto'
a.src = url
}
}
export function stopSpeak() {
if (innerAudio) innerAudio.stop()
if (typeof window !== 'undefined' && window.speechSynthesis) {