fix: Fix some phones unable to play audio (#2728)

This commit is contained in:
shaohuzhang1 2025-03-28 18:24:00 +08:00 committed by GitHub
parent cc2789f37f
commit bf91579b4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 27 deletions

View File

@ -94,15 +94,6 @@
</el-button> </el-button>
</el-tooltip> </el-tooltip>
</span> </span>
<!-- 先渲染不然不能播放 -->
<audio
ref="audioPlayer"
v-for="item in audioList"
:key="item"
controls
hidden="hidden"
></audio>
<div ref="audioCiontainer"></div> <div ref="audioCiontainer"></div>
</div> </div>
</div> </div>
@ -293,7 +284,7 @@ class AudioManage {
index = this.textList.length - 1 index = this.textList.length - 1
if (this.ttsType === 'TTS') { if (this.ttsType === 'TTS') {
const audioElement: HTMLAudioElement = document.createElement('audio') const audioElement: HTMLAudioElement = document.createElement('audio')
audioElement.controls = true audioElement.controls = false
audioElement.hidden = true audioElement.hidden = true
/** /**
* 播放结束事件 * 播放结束事件
@ -436,7 +427,21 @@ class AudioManage {
} }
const audioElement = this.audioList[index] const audioElement = this.audioList[index]
if (audioElement instanceof SpeechSynthesisUtterance) {
if (audioElement instanceof HTMLAudioElement) {
//
try {
this.statusList[index] = AudioStatus.PLAY_INT
const play = audioElement.play()
if (play instanceof Promise) {
play.catch((e) => {
this.statusList[index] = AudioStatus.READY
})
}
} catch (e: any) {
this.statusList[index] = AudioStatus.ERROR
}
} else {
if (window.speechSynthesis.paused) { if (window.speechSynthesis.paused) {
window.speechSynthesis.resume() window.speechSynthesis.resume()
} else { } else {
@ -446,14 +451,6 @@ class AudioManage {
speechSynthesis.speak(audioElement) speechSynthesis.speak(audioElement)
this.statusList[index] = AudioStatus.PLAY_INT this.statusList[index] = AudioStatus.PLAY_INT
} }
} else {
//
try {
audioElement.play()
this.statusList[index] = AudioStatus.PLAY_INT
} catch (e) {
this.statusList[index] = AudioStatus.ERROR
}
} }
} }
pause(self?: boolean) { pause(self?: boolean) {
@ -462,7 +459,14 @@ class AudioManage {
return return
} }
const audioElement = this.audioList[index] const audioElement = this.audioList[index]
if (audioElement instanceof SpeechSynthesisUtterance) {
if (audioElement instanceof HTMLAudioElement) {
if (this.statusList[index] === AudioStatus.PLAY_INT) {
//
this.statusList[index] = AudioStatus.READY
audioElement.pause()
}
} else {
this.statusList[index] = AudioStatus.READY this.statusList[index] = AudioStatus.READY
if (self) { if (self) {
window.speechSynthesis.pause() window.speechSynthesis.pause()
@ -474,12 +478,6 @@ class AudioManage {
} else { } else {
window.speechSynthesis.cancel() window.speechSynthesis.cancel()
} }
} else {
if (this.statusList[index] === AudioStatus.PLAY_INT) {
//
this.statusList[index] = AudioStatus.READY
audioElement.pause()
}
} }
} }
getTextList(text: string, is_end: boolean) { getTextList(text: string, is_end: boolean) {

View File

@ -523,7 +523,10 @@ onMounted(() => {
let userFormData = JSON.parse(localStorage.getItem(`${accessToken}userForm`) || '{}') let userFormData = JSON.parse(localStorage.getItem(`${accessToken}userForm`) || '{}')
form_data.value = userFormData form_data.value = userFormData
} }
window.speechSynthesis.cancel() if (window.speechSynthesis) {
window.speechSynthesis.cancel()
}
window.sendMessage = sendMessage window.sendMessage = sendMessage
bus.on('on:transcribing', (status: boolean) => { bus.on('on:transcribing', (status: boolean) => {
transcribing.value = status transcribing.value = status