修复
This commit is contained in:
@@ -215,8 +215,10 @@ export function useTTS(options = {}) {
|
||||
* @param {Object} voice - 音色对象
|
||||
* @param {Function} onSuccess - 成功回调
|
||||
* @param {Function} onError - 错误回调
|
||||
* @param {Object} options - 选项
|
||||
* @param {boolean} options.autoPlay - 是否自动播放(默认 true)
|
||||
*/
|
||||
async function playVoiceSample(voice, onSuccess, onError) {
|
||||
async function playVoiceSample(voice, onSuccess, onError, options = { autoPlay: true }) {
|
||||
if (!voice) return
|
||||
if (previewLoadingVoiceId.value === voice.id || playingPreviewVoiceId.value === voice.id) {
|
||||
return
|
||||
@@ -237,7 +239,11 @@ export function useTTS(options = {}) {
|
||||
const cachedAudio = previewAudioCache.get(cacheKey)
|
||||
|
||||
if (cachedAudio) {
|
||||
playCachedAudio(cachedAudio, resetPreviewState)
|
||||
if (options.autoPlay !== false) {
|
||||
playCachedAudio(cachedAudio, resetPreviewState)
|
||||
} else {
|
||||
resetPreviewState()
|
||||
}
|
||||
onSuccess && onSuccess(cachedAudio)
|
||||
return
|
||||
}
|
||||
@@ -260,19 +266,23 @@ export function useTTS(options = {}) {
|
||||
|
||||
if (res.data?.audioUrl) {
|
||||
resetPreviewState()
|
||||
playAudioPreview(res.data.audioUrl, {
|
||||
revokeOnEnd: true,
|
||||
onEnded: function() {
|
||||
URL.revokeObjectURL(res.data.audioUrl)
|
||||
}
|
||||
})
|
||||
if (options.autoPlay !== false) {
|
||||
playAudioPreview(res.data.audioUrl, {
|
||||
revokeOnEnd: true,
|
||||
onEnded: function() {
|
||||
URL.revokeObjectURL(res.data.audioUrl)
|
||||
}
|
||||
})
|
||||
}
|
||||
onSuccess?.(res.data)
|
||||
} else if (res.data?.audioBase64) {
|
||||
const audioData = await decodeAndCacheBase64(res.data.audioBase64, res.data.format, cacheKey)
|
||||
resetPreviewState()
|
||||
playCachedAudio(audioData, function() {
|
||||
URL.revokeObjectURL(audioData.objectUrl)
|
||||
})
|
||||
if (options.autoPlay !== false) {
|
||||
playCachedAudio(audioData, function() {
|
||||
URL.revokeObjectURL(audioData.objectUrl)
|
||||
})
|
||||
}
|
||||
onSuccess?.(audioData)
|
||||
} else {
|
||||
message.error('试听失败')
|
||||
|
||||
Reference in New Issue
Block a user