Skip to content

Commit ac515dc

Browse files
committed
fix: Fix some phones unable to play audio
1 parent dcc80a4 commit ac515dc

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

ui/src/components/ai-chat/component/operation-button/ChatOperationButton.vue

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,6 @@
9494
</el-button>
9595
</el-tooltip>
9696
</span>
97-
98-
<!-- 先渲染,不然不能播放 -->
99-
<audio
100-
ref="audioPlayer"
101-
v-for="item in audioList"
102-
:key="item"
103-
controls
104-
hidden="hidden"
105-
></audio>
10697
<div ref="audioCiontainer"></div>
10798
</div>
10899
</div>
@@ -293,7 +284,7 @@ class AudioManage {
293284
index = this.textList.length - 1
294285
if (this.ttsType === 'TTS') {
295286
const audioElement: HTMLAudioElement = document.createElement('audio')
296-
audioElement.controls = true
287+
audioElement.controls = false
297288
audioElement.hidden = true
298289
/**
299290
* 播放结束事件
@@ -436,7 +427,21 @@ class AudioManage {
436427
}
437428
438429
const audioElement = this.audioList[index]
439-
if (audioElement instanceof SpeechSynthesisUtterance) {
430+
431+
if (audioElement instanceof HTMLAudioElement) {
432+
// 标签朗读
433+
try {
434+
this.statusList[index] = AudioStatus.PLAY_INT
435+
const play = audioElement.play()
436+
if (play instanceof Promise) {
437+
play.catch((e) => {
438+
this.statusList[index] = AudioStatus.READY
439+
})
440+
}
441+
} catch (e: any) {
442+
this.statusList[index] = AudioStatus.ERROR
443+
}
444+
} else {
440445
if (window.speechSynthesis.paused) {
441446
window.speechSynthesis.resume()
442447
} else {
@@ -446,14 +451,6 @@ class AudioManage {
446451
speechSynthesis.speak(audioElement)
447452
this.statusList[index] = AudioStatus.PLAY_INT
448453
}
449-
} else {
450-
// 标签朗读
451-
try {
452-
audioElement.play()
453-
this.statusList[index] = AudioStatus.PLAY_INT
454-
} catch (e) {
455-
this.statusList[index] = AudioStatus.ERROR
456-
}
457454
}
458455
}
459456
pause(self?: boolean) {
@@ -462,7 +459,14 @@ class AudioManage {
462459
return
463460
}
464461
const audioElement = this.audioList[index]
465-
if (audioElement instanceof SpeechSynthesisUtterance) {
462+
463+
if (audioElement instanceof HTMLAudioElement) {
464+
if (this.statusList[index] === AudioStatus.PLAY_INT) {
465+
// 标签朗读
466+
this.statusList[index] = AudioStatus.READY
467+
audioElement.pause()
468+
}
469+
} else {
466470
this.statusList[index] = AudioStatus.READY
467471
if (self) {
468472
window.speechSynthesis.pause()
@@ -474,12 +478,6 @@ class AudioManage {
474478
} else {
475479
window.speechSynthesis.cancel()
476480
}
477-
} else {
478-
if (this.statusList[index] === AudioStatus.PLAY_INT) {
479-
// 标签朗读
480-
this.statusList[index] = AudioStatus.READY
481-
audioElement.pause()
482-
}
483481
}
484482
}
485483
getTextList(text: string, is_end: boolean) {

ui/src/components/ai-chat/index.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,10 @@ onMounted(() => {
523523
let userFormData = JSON.parse(localStorage.getItem(`${accessToken}userForm`) || '{}')
524524
form_data.value = userFormData
525525
}
526-
window.speechSynthesis.cancel()
526+
if (window.speechSynthesis) {
527+
window.speechSynthesis.cancel()
528+
}
529+
527530
window.sendMessage = sendMessage
528531
bus.on('on:transcribing', (status: boolean) => {
529532
transcribing.value = status

0 commit comments

Comments
 (0)