fix: 首页TTS同样改用blob播放,解决downloadFile导致页面刷新的问题
This commit is contained in:
@@ -487,12 +487,39 @@ const playTryResult = () => {
|
||||
const hasChinese = /[\u4e00-\u9fa5]/.test(tryText.value)
|
||||
const lang = hasChinese ? 'en' : 'zh'
|
||||
const token = uni.getStorageSync('token')
|
||||
const url = `${BASE_URL}/translate/tts?text=${encodeURIComponent(tryResult.value)}&lang=${lang}`
|
||||
const text = tryResult.value
|
||||
|
||||
uni.showLoading({ title: '语音生成中...' })
|
||||
|
||||
if (typeof window !== 'undefined' && window.Audio) {
|
||||
uni.request({
|
||||
url: `${BASE_URL}/translate/tts?text=${encodeURIComponent(text)}&lang=${lang}`,
|
||||
method: 'GET',
|
||||
header: { Authorization: `Bearer ${token}` },
|
||||
responseType: 'arraybuffer',
|
||||
success: (res) => {
|
||||
uni.hideLoading()
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
const blob = new Blob([res.data], { type: 'audio/mpeg' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const audio = new Audio(url)
|
||||
audio.onended = () => { URL.revokeObjectURL(url) }
|
||||
audio.play().catch(() => {
|
||||
uni.showToast({ title: '播放失败,请检查音量', icon: 'none' })
|
||||
})
|
||||
} else {
|
||||
uni.showToast({ title: '语音生成失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: '语音生成失败', icon: 'none' })
|
||||
},
|
||||
})
|
||||
} else {
|
||||
uni.downloadFile({
|
||||
url,
|
||||
header: token ? { Authorization: `Bearer ${token}` } : {},
|
||||
url: `${BASE_URL}/translate/tts?text=${encodeURIComponent(text)}&lang=${lang}`,
|
||||
header: { Authorization: `Bearer ${token}` },
|
||||
success: (res) => {
|
||||
uni.hideLoading()
|
||||
if (res.statusCode === 200) {
|
||||
@@ -509,6 +536,7 @@ const playTryResult = () => {
|
||||
uni.showToast({ title: '语音生成失败', icon: 'none' })
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user