Feat(AI):TTS功能实现
This commit is contained in:
@@ -50,7 +50,9 @@ export const chatMixins = {
|
||||
],
|
||||
errorMsg: Object.freeze('您提的问题未能理解,云灵思想跑偏啦,重新整理一下问题或者重新提问,让云灵再试试!'),
|
||||
audioAyy: [],
|
||||
isPlayAudio: false
|
||||
isPlayAudio: false,
|
||||
audioContext: null,
|
||||
audioPlayId: ''
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@@ -408,9 +410,38 @@ export const chatMixins = {
|
||||
}
|
||||
},
|
||||
ttsHandle(item) {
|
||||
this.audioAyy = []
|
||||
if (this.audioContext) {
|
||||
this.audioContext.stop()
|
||||
// this.audioContext.destroy()
|
||||
}
|
||||
// 如果正在播放,点击当前播放的则认为是暂停
|
||||
if (this.isPlayAudio && item.conversationId === this.audioPlayId) {
|
||||
this.isPlayAudio = false
|
||||
this.audioPlayId = ''
|
||||
return
|
||||
}
|
||||
this.isPlayAudio = false
|
||||
this.audioAyy = []
|
||||
this.splitAndSynthesize(removeMarkdown(item.content).replace(/[\n\t\s]/g, ''))
|
||||
this.audioPlayId = item.conversationId
|
||||
const flag = 1
|
||||
if (flag === 1) {
|
||||
console.log(removeMarkdown(item.content).replace(/[\n\t\s]/g, ''))
|
||||
uni.showLoading({
|
||||
title: '合成中...'
|
||||
})
|
||||
this.splitAndSynthesize(removeMarkdown(item.content).replace(/[\n\t\s]/g, ''))
|
||||
} else {
|
||||
// 调试数据
|
||||
this.audioAyy = [
|
||||
'https://ae.weixin.qq.com/cgi-bin/mmasrai-bin/getmedia?filename=1750749075_2f71887cc48d9b3753c39d119862d4bb&filekey=871027724&source=miniapp_plugin',
|
||||
'https://ae.weixin.qq.com/cgi-bin/mmasrai-bin/getmedia?filename=1750749077_0970508786c5e3edc3a68a5eb9de048c&filekey=871027724&source=miniapp_plugin',
|
||||
'https://ae.weixin.qq.com/cgi-bin/mmasrai-bin/getmedia?filename=1750749079_e77cec52cc077ba981892887c3df4cdf&filekey=871027724&source=miniapp_plugin',
|
||||
'https://ae.weixin.qq.com/cgi-bin/mmasrai-bin/getmedia?filename=1750749080_02db4235771f5561cdb73c06d8c67e53&filekey=871027724&source=miniapp_plugin',
|
||||
'https://ae.weixin.qq.com/cgi-bin/mmasrai-bin/getmedia?filename=1750749082_534e455c5d6ac5e9a4192ecf9952851a&filekey=871027724&source=miniapp_plugin',
|
||||
'https://ae.weixin.qq.com/cgi-bin/mmasrai-bin/getmedia?filename=1750749083_78c4a2fecad538561f81c48dc4ecd645&filekey=871027724&source=miniapp_plugin'
|
||||
]
|
||||
this.playNextChunk()
|
||||
}
|
||||
},
|
||||
splitAndSynthesize(text) {
|
||||
const _this = this
|
||||
@@ -440,25 +471,38 @@ export const chatMixins = {
|
||||
_this.playNextChunk()
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
},
|
||||
fail: function(err) {
|
||||
console.log("转换失败:", err)
|
||||
uni.hideLoading()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
if (!_this.isPlayAudio) {
|
||||
_this.playNextChunk()
|
||||
}
|
||||
uni.hideLoading()
|
||||
}
|
||||
},
|
||||
playNextChunk() {
|
||||
if (this.audioAyy.length > 0) {
|
||||
if (this.audioAyy.length > 0 && this.audioPlayId) {
|
||||
const src = this.audioAyy.shift()
|
||||
this.isPlayAudio = true
|
||||
const audioContext = uni.createInnerAudioContext()
|
||||
audioContext.src = this.audioAyy.shift()
|
||||
audioContext.onEnded(() => this.playNextChunk())
|
||||
const audioContext = uni.createInnerAudioContext({
|
||||
// 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长,因此对于长音频建议关闭此选项
|
||||
useWebAudioImplement: false
|
||||
})
|
||||
this.audioContext = audioContext
|
||||
audioContext.src = src
|
||||
audioContext.onEnded(() => {
|
||||
audioContext.destroy()
|
||||
this.playNextChunk()
|
||||
})
|
||||
audioContext.play()
|
||||
console.log('------------src:' + src)
|
||||
} else {
|
||||
console.log('------------end')
|
||||
this.isPlayAudio = false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user