flat: 添加语音播放提示

This commit is contained in:
Apcallover
2025-12-20 15:10:44 +08:00
parent 02fef1700b
commit 1bacbe4936
5 changed files with 97 additions and 29 deletions

View File

@@ -6,6 +6,69 @@ import {
onHide,
onUnload
} from '@dcloudio/uni-app'
import {
isY9MachineType
} from '../common/globalFunction';
/**
* 封装 hh.call 通用调用
*/
const callBridge = (params) => {
return new Promise((resolve) => {
if (typeof window !== 'undefined' && window.hh && window.hh.call) {
// 打印日志方便调试
console.log('[TTS Bridge Send]:', params)
window.hh.call("ampeHHCommunication", params, (res) => {
console.log('[TTS Bridge Res]:', res)
resolve(res)
})
} else {
console.warn('当前环境不支持 hh.call模拟成功')
resolve({
success: true
})
}
})
}
/**
* 生成随机 TaskId
*/
const generateTaskId = () => {
return 'task_' + Date.now() + '_' + Math.floor(Math.random() * 1000)
}
/**
* 直接播放文本 (静态方法,无需实例化 Hook)
* @param {string} text - 需要朗读的文本
*/
export async function playTextDirectly(text) {
if (!text) return
if (!isY9MachineType()) return
const processedText = extractSpeechText(text)
if (!processedText) return
try {
// 构造播放参数
const params = {
"action": "speech",
"event": "open",
"taskId": generateTaskId(),
"params": {
"text": processedText,
"pitch": 1.0,
"rate": 1.0,
"speechQueue": 1 // 1表示打断当前播放立即播放新的
}
}
// 直接调用
await callBridge(params)
} catch (e) {
console.error('Direct Play Error:', e)
}
}
/**
* 一体机 TTS 播放器 Hook (修复版)
@@ -19,34 +82,6 @@ export function useTTSPlayer() {
// 记录最后一次播放的文本
const lastText = ref('')
/**
* 封装 hh.call 通用调用
*/
const callBridge = (params) => {
return new Promise((resolve) => {
if (typeof window !== 'undefined' && window.hh && window.hh.call) {
// 打印日志方便调试
console.log('[TTS Bridge Send]:', params)
window.hh.call("ampeHHCommunication", params, (res) => {
console.log('[TTS Bridge Res]:', res)
resolve(res)
})
} else {
console.warn('当前环境不支持 hh.call模拟成功')
resolve({
success: true
})
}
})
}
/**
* 生成随机 TaskId
*/
const generateTaskId = () => {
return 'task_' + Date.now() + '_' + Math.floor(Math.random() * 1000)
}
/**
* 停止 (中断) - 内部使用
*/
@@ -221,4 +256,19 @@ function extractSpeechText(markdown) {
finalTextParts.push(...jobTexts);
if (endingText) finalTextParts.push(endingText);
return finalTextParts.join('\n');
}
}
// 使用方法 1
// import { useTTSPlayer } from '@/hook/useTTSPlayer-all-in-one'
// const { speak, stop, isSpeaking } = useTTSPlayer()
// // 调用
// speak('你好,这是一段测试文本')
// 使用方法 2
// import { playTextDirectly } from '@/hook/useTTSPlayer-all-in-one'
// // 直接调用即可,无需实例化
// playTextDirectly('直接朗读这段话不需要处理暂停和UI状态')