5454 <EditContentDialog ref =" EditContentDialogRef" @refresh =" refreshContent" />
5555 <EditMarkDialog ref =" EditMarkDialogRef" @refresh =" refreshMark" />
5656 <!-- 先渲染,不然不能播放 -->
57- <audio ref =" audioPlayer" controls hidden =" hidden" ></audio >
57+ <audio ref =" audioPlayer" v-for = " item in audioList " :key = " item " controls hidden =" hidden" ></audio >
5858 </div >
5959 </div >
6060</template >
@@ -88,14 +88,16 @@ const props = defineProps({
8888
8989const emit = defineEmits ([' update:data' ])
9090
91- const audioPlayer = ref <HTMLAudioElement | null >(null )
91+ const audioPlayer = ref <HTMLAudioElement [] | null >(null )
9292
9393const EditContentDialogRef = ref ()
9494const EditMarkDialogRef = ref ()
9595
9696const buttonData = ref (props .data )
9797const loading = ref (false )
9898const utterance = ref <SpeechSynthesisUtterance | null >(null )
99+ const audioList = ref <string []>([])
100+ const currentAudioIndex = ref (0 )
99101
100102function editContent(data : any ) {
101103 EditContentDialogRef .value .open (data )
@@ -149,35 +151,56 @@ const playAnswerText = (text: string) => {
149151 text = markdownToPlainText (text )
150152 // console.log(text)
151153 audioPlayerStatus .value = true
152- if (props .tts_type === ' BROWSER' ) {
153- if (text !== utterance .value ?.text ) {
154+ // 分割成多份
155+ audioList .value = text .split (/ (<audio[^ >] * ><\/ audio>)/ )
156+ playAnswerTextPart ()
157+ }
158+
159+ const playAnswerTextPart = () => {
160+ // console.log(audioList.value, currentAudioIndex.value)
161+ if (currentAudioIndex .value === audioList .value .length ) {
162+ audioPlayerStatus .value = false
163+ currentAudioIndex .value = 0
164+ return
165+ }
166+ if (audioList .value [currentAudioIndex .value ].includes (' <audio' )) {
167+ if (audioPlayer .value ) {
168+ audioPlayer .value [currentAudioIndex .value ].src = audioList .value [currentAudioIndex .value ].match (/ src="([^ "] * )"/ )?.[1 ] || ' '
169+ audioPlayer .value [currentAudioIndex .value ].play () // 自动播放音频
170+ audioPlayer .value [currentAudioIndex .value ].onended = () => {
171+ currentAudioIndex .value += 1
172+ playAnswerTextPart ()
173+ }
174+ }
175+ } else if (props .tts_type === ' BROWSER' ) {
176+ if (audioList .value [currentAudioIndex .value ] !== utterance .value ?.text ) {
154177 window .speechSynthesis .cancel ()
155178 }
156179 if (window .speechSynthesis .paused ) {
157180 window .speechSynthesis .resume ()
158181 return
159182 }
160183 // 创建一个新的 SpeechSynthesisUtterance 实例
161- utterance .value = new SpeechSynthesisUtterance (text )
184+ utterance .value = new SpeechSynthesisUtterance (audioList . value [ currentAudioIndex . value ] )
162185 utterance .value .onend = () => {
163- audioPlayerStatus .value = false
164186 utterance .value = null
187+ currentAudioIndex .value += 1
188+ playAnswerTextPart ()
165189 }
166190 utterance .value .onerror = () => {
167191 audioPlayerStatus .value = false
168192 utterance .value = null
169193 }
170194 // 调用浏览器的朗读功能
171195 window .speechSynthesis .speak (utterance .value )
172- }
173- if (props .tts_type === ' TTS' ) {
196+ } else if (props .tts_type === ' TTS' ) {
174197 // 恢复上次暂停的播放
175- if (audioPlayer .value ?.src ) {
176- audioPlayer .value ? .play ()
198+ if (audioPlayer .value && audioPlayer . value [ currentAudioIndex . value ] ?.src ) {
199+ audioPlayer .value [ currentAudioIndex . value ] .play ()
177200 return
178201 }
179202 applicationApi
180- .postTextToSpeech (id || (props . applicationId as string ), { text: text }, loading )
203+ .postTextToSpeech (( props . applicationId as string ) || (id as string ), { text: audioList . value [ currentAudioIndex . value ] }, loading )
181204 .then (async (res : any ) => {
182205 if (res .type === ' application/json' ) {
183206 const text = await res .text ()
@@ -198,11 +221,12 @@ const playAnswerText = (text: string) => {
198221 // link.click()
199222
200223 // 检查 audioPlayer 是否已经引用了 DOM 元素
201- if (audioPlayer .value instanceof HTMLAudioElement ) {
202- audioPlayer .value .src = url
203- audioPlayer .value .play () // 自动播放音频
204- audioPlayer .value .onended = () => {
205- audioPlayerStatus .value = false
224+ if (audioPlayer .value ) {
225+ audioPlayer .value [currentAudioIndex .value ].src = url
226+ audioPlayer .value [currentAudioIndex .value ].play () // 自动播放音频
227+ audioPlayer .value [currentAudioIndex .value ].onended = () => {
228+ currentAudioIndex .value += 1
229+ playAnswerTextPart ()
206230 }
207231 } else {
208232 console .error (' audioPlayer.value is not an instance of HTMLAudioElement' )
@@ -217,13 +241,18 @@ const playAnswerText = (text: string) => {
217241const pausePlayAnswerText = () => {
218242 audioPlayerStatus .value = false
219243 if (props .tts_type === ' TTS' ) {
220- audioPlayer .value ?.pause ()
244+ if (audioPlayer .value ) {
245+ audioPlayer .value ?.forEach ((item ) => {
246+ item .pause ()
247+ })
248+ }
221249 }
222250 if (props .tts_type === ' BROWSER' ) {
223251 window .speechSynthesis .pause ()
224252 }
225253}
226254
255+
227256function refreshMark() {
228257 buttonData .value .improve_paragraph_id_list = []
229258 emit (' update:data' , buttonData .value )
0 commit comments