feat: 优化

This commit is contained in:
2026-02-26 18:52:09 +08:00
parent c2e4fde218
commit b76e3ff47d
17 changed files with 1027 additions and 1630 deletions

View File

@@ -89,15 +89,15 @@ export function useTTS(options = {}) {
}
audio.play()
.then(function() {
.then(() => {
previewAudio = audio
audio.onended = cleanup
audio.onerror = function() {
audio.onerror = () => {
cleanup()
message.error('播放失败')
}
})
.catch(function() {
.catch(() => {
cleanup()
message.error('播放失败')
})
@@ -159,11 +159,11 @@ export function useTTS(options = {}) {
playAudioPreview(previewObjectUrl, {
revokeOnEnd: false,
onEnded: function() {
onEnded() {
if (audioData.objectUrl?.startsWith('blob:')) {
URL.revokeObjectURL(audioData.objectUrl)
}
onEnded && onEnded()
onEnded?.()
}
})
}
@@ -244,7 +244,7 @@ export function useTTS(options = {}) {
} else {
resetPreviewState()
}
onSuccess && onSuccess(cachedAudio)
onSuccess?.(cachedAudio)
return
}
@@ -252,11 +252,11 @@ export function useTTS(options = {}) {
const params = buildPreviewParams(voice)
if (!params) {
resetPreviewState()
onError && onError(new Error('参数构建失败'))
onError?.(new Error('参数构建失败'))
return
}
const res = await VoiceService.preview(params)
const res = await VoiceService.synthesize(params)
if (res.code !== 0) {
message.error(res.msg || '试听失败')
resetPreviewState()
@@ -269,7 +269,7 @@ export function useTTS(options = {}) {
if (options.autoPlay !== false) {
playAudioPreview(res.data.audioUrl, {
revokeOnEnd: true,
onEnded: function() {
onEnded() {
URL.revokeObjectURL(res.data.audioUrl)
}
})
@@ -279,7 +279,7 @@ export function useTTS(options = {}) {
const audioData = await decodeAndCacheBase64(res.data.audioBase64, res.data.format, cacheKey)
resetPreviewState()
if (options.autoPlay !== false) {
playCachedAudio(audioData, function() {
playCachedAudio(audioData, () => {
URL.revokeObjectURL(audioData.objectUrl)
})
}
@@ -292,7 +292,7 @@ export function useTTS(options = {}) {
} catch (error) {
message.error('试听失败')
resetPreviewState()
onError && onError(error)
onError?.(error)
}
}
@@ -335,7 +335,7 @@ export function useTTS(options = {}) {
* 清除音频缓存
*/
function clearAudioCache() {
previewAudioCache.forEach(function(audioData) {
previewAudioCache.forEach((audioData) => {
URL.revokeObjectURL(audioData.objectUrl)
})
previewAudioCache.clear()