fix: Voice playback may cause playback issues (#2689)

This commit is contained in:
shaohuzhang1 2025-03-26 13:49:30 +08:00 committed by GitHub
parent d27e5a4c01
commit 394d96980b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -142,7 +142,6 @@ const emit = defineEmits(['update:data', 'regeneration'])
const audioPlayer = ref<HTMLAudioElement[] | null>([]) const audioPlayer = ref<HTMLAudioElement[] | null>([])
const audioCiontainer = ref<HTMLDivElement>() const audioCiontainer = ref<HTMLDivElement>()
const audioPlayerStatus = ref(false)
const buttonData = ref(props.data) const buttonData = ref(props.data)
const loading = ref(false) const loading = ref(false)
@ -271,6 +270,7 @@ class AudioManage {
tryList: Array<number> tryList: Array<number>
ttsType: string ttsType: string
root: Element root: Element
is_end: boolean
constructor(ttsType: string, root: HTMLDivElement) { constructor(ttsType: string, root: HTMLDivElement) {
this.textList = [] this.textList = []
this.audioList = [] this.audioList = []
@ -278,6 +278,7 @@ class AudioManage {
this.tryList = [] this.tryList = []
this.ttsType = ttsType this.ttsType = ttsType
this.root = root this.root = root
this.is_end = false
} }
appendTextList(textList: Array<string>) { appendTextList(textList: Array<string>) {
const newTextList = textList.slice(this.textList.length) const newTextList = textList.slice(this.textList.length)
@ -300,8 +301,9 @@ class AudioManage {
audioElement.onended = () => { audioElement.onended = () => {
this.statusList[index] = AudioStatus.END this.statusList[index] = AudioStatus.END
// //
if (this.statusList.every((item) => item === AudioStatus.END)) { if (this.statusList.every((item) => item === AudioStatus.END) && this.is_end) {
this.statusList = this.statusList.map((item) => AudioStatus.READY) this.statusList = this.statusList.map((item) => AudioStatus.READY)
this.is_end = false
} else { } else {
// next // next
this.play() this.play()
@ -323,13 +325,11 @@ class AudioManage {
const text = await res.text() const text = await res.text()
MsgError(text) MsgError(text)
this.statusList[index] = AudioStatus.ERROR this.statusList[index] = AudioStatus.ERROR
this.play() throw ''
return
} }
// MP3 // MP3
// Blob // Blob
const blob = new Blob([res], { type: 'audio/mp3' }) const blob = new Blob([res], { type: 'audio/mp3' })
// URL // URL
const url = URL.createObjectURL(blob) const url = URL.createObjectURL(blob)
audioElement.src = url audioElement.src = url
@ -337,7 +337,6 @@ class AudioManage {
this.play() this.play()
}) })
.catch((err) => { .catch((err) => {
console.log('err: ', err)
this.statusList[index] = AudioStatus.ERROR this.statusList[index] = AudioStatus.ERROR
this.play() this.play()
}) })
@ -348,9 +347,6 @@ class AudioManage {
const speechSynthesisUtterance: SpeechSynthesisUtterance = new SpeechSynthesisUtterance( const speechSynthesisUtterance: SpeechSynthesisUtterance = new SpeechSynthesisUtterance(
text text
) )
speechSynthesisUtterance.onpause = () => {
console.log('onpause')
}
speechSynthesisUtterance.onend = () => { speechSynthesisUtterance.onend = () => {
this.statusList[index] = AudioStatus.END this.statusList[index] = AudioStatus.END
// //
@ -389,8 +385,8 @@ class AudioManage {
if (res.type === 'application/json') { if (res.type === 'application/json') {
const text = await res.text() const text = await res.text()
MsgError(text) MsgError(text)
this.statusList[index] = AudioStatus.ERROR
return throw ''
} }
// MP3 // MP3
// Blob // Blob
@ -405,6 +401,7 @@ class AudioManage {
.catch((err) => { .catch((err) => {
console.log('err: ', err) console.log('err: ', err)
this.statusList[index] = AudioStatus.ERROR this.statusList[index] = AudioStatus.ERROR
this.play()
}) })
} }
} }
@ -414,6 +411,9 @@ class AudioManage {
return this.statusList.some((item) => [AudioStatus.PLAY_INT].includes(item)) return this.statusList.some((item) => [AudioStatus.PLAY_INT].includes(item))
} }
play(text?: string, is_end?: boolean, self?: boolean) { play(text?: string, is_end?: boolean, self?: boolean) {
if (is_end) {
this.is_end = true
}
if (self) { if (self) {
this.tryList = this.tryList.map((item) => 0) this.tryList = this.tryList.map((item) => 0)
} }
@ -431,7 +431,6 @@ class AudioManage {
const index = this.statusList.findIndex((status) => const index = this.statusList.findIndex((status) =>
[AudioStatus.MOUNTED, AudioStatus.READY].includes(status) [AudioStatus.MOUNTED, AudioStatus.READY].includes(status)
) )
if (index < 0 || this.statusList[index] === AudioStatus.MOUNTED) { if (index < 0 || this.statusList[index] === AudioStatus.MOUNTED) {
return return
} }
@ -497,6 +496,7 @@ class AudioManage {
}, },
is_end is_end
) )
return split return split
} }
} }