Files
qingdao-employment-service/hook/useTTSPlayer2.js

216 lines
5.9 KiB
JavaScript
Raw Normal View History

2025-12-06 14:18:33 +08:00
import {
ref,
onUnmounted,
2025-12-07 17:06:20 +08:00
onMounted,
watch
2025-12-06 14:18:33 +08:00
} from 'vue'
import {
onHide,
onUnload
} from '@dcloudio/uni-app'
import config from '@/config'
2025-12-07 17:06:20 +08:00
// 请确保 piper-sdk.js 已经正确 export class PiperTTS
import {
PiperTTS
} from './piper-sdk.js'
2025-12-06 14:18:33 +08:00
export function useTTSPlayer() {
2025-12-07 17:06:20 +08:00
// UI 状态
2025-12-06 14:18:33 +08:00
const isSpeaking = ref(false)
const isPaused = ref(false)
const isLoading = ref(false)
2025-12-07 17:06:20 +08:00
// SDK 实例
let piper = null
2025-12-06 14:18:33 +08:00
2025-12-07 17:06:20 +08:00
/**
* 初始化 SDK 实例
* 每次 stop piper 会被置空这里会重新创建
*/
const initPiper = () => {
if (piper) return
2025-12-06 14:18:33 +08:00
2025-12-07 17:06:20 +08:00
let baseUrl = config.speechSynthesis2 || ''
baseUrl = baseUrl.replace(/\/$/, '')
2025-12-06 14:18:33 +08:00
2025-12-07 17:06:20 +08:00
piper = new PiperTTS({
baseUrl: baseUrl,
onStatus: (msg, type) => {
if (type === 'error') {
console.error('[TTS Error]', msg)
// 出错时不重置状态,交给用户手动处理或结束事件处理
resetState()
}
},
onStart: () => {
isLoading.value = false
isSpeaking.value = true
isPaused.value = false
},
onEnd: () => {
2025-12-06 14:18:33 +08:00
resetState()
2025-12-07 17:06:20 +08:00
}
})
2025-12-06 14:18:33 +08:00
}
/**
2025-12-07 17:06:20 +08:00
* 核心朗读方法
2025-12-06 14:18:33 +08:00
*/
const speak = async (text) => {
if (!text) return
const processedText = extractSpeechText(text)
if (!processedText) return
2025-12-07 17:06:20 +08:00
// 1. 【关键修改】先彻底停止并销毁旧实例
// 这会断开 socket 并且 close AudioContext确保上一个声音立即消失
await stop()
// 2. 初始化新实例 (因为 stop() 把 piper 设为了 null)
initPiper()
2025-12-06 14:18:33 +08:00
2025-12-07 17:06:20 +08:00
// 3. 更新 UI 为加载中
2025-12-06 14:18:33 +08:00
isLoading.value = true
isPaused.value = false
2025-12-07 17:06:20 +08:00
isSpeaking.value = true // 预先设为 true防止按钮闪烁
2025-12-06 14:18:33 +08:00
try {
2025-12-07 17:06:20 +08:00
// 4. 激活音频引擎 (移动端防静音关键)
await piper.init()
// 5. 发送请求
piper.speak(processedText, {
speakerId: 0,
noiseScale: 0.667,
lengthScale: 1.0
2025-12-06 14:18:33 +08:00
})
} catch (e) {
2025-12-07 17:06:20 +08:00
console.error('TTS Speak Error:', e)
resetState()
2025-12-06 14:18:33 +08:00
}
}
2025-12-07 17:06:20 +08:00
/**
* 暂停
*/
const pause = async () => {
if (piper && piper.audioCtx && piper.audioCtx.state === 'running') {
await piper.audioCtx.suspend()
2025-12-06 14:18:33 +08:00
isPaused.value = true
}
}
2025-12-07 17:06:20 +08:00
/**
* 恢复
*/
const resume = async () => {
if (piper && piper.audioCtx && piper.audioCtx.state === 'suspended') {
await piper.audioCtx.resume()
2025-12-06 14:18:33 +08:00
isPaused.value = false
isSpeaking.value = true
}
}
2025-12-07 17:06:20 +08:00
/**
* 停止并重置 (核打击模式)
*/
const stop = async () => {
if (piper) {
// 1. 断开 WebSocket
piper.stop()
// 2. 【关键】关闭 AudioContext
// Web Audio API 中,已经 schedule 的 buffer 很难单独取消
// 最直接的方法是关闭整个 Context
if (piper.audioCtx && piper.audioCtx.state !== 'closed') {
try {
await piper.audioCtx.close()
} catch (e) {
console.warn('AudioContext close failed', e)
}
2025-12-06 14:18:33 +08:00
}
2025-12-07 17:06:20 +08:00
// 3. 销毁实例引用
piper = null
2025-12-06 14:18:33 +08:00
}
resetState()
}
2025-12-07 17:06:20 +08:00
// UI 状态重置
2025-12-06 14:18:33 +08:00
const resetState = () => {
isSpeaking.value = false
isPaused.value = false
isLoading.value = false
}
// === 生命周期 ===
onMounted(() => {
2025-12-07 17:06:20 +08:00
// 预初始化可以不做,等到点击时再做,避免空闲占用 AudioContext 资源
// initPiper()
2025-12-06 14:18:33 +08:00
})
onUnmounted(() => {
2025-12-07 17:06:20 +08:00
stop()
2025-12-06 14:18:33 +08:00
})
2025-12-07 17:06:20 +08:00
// Uniapp 生命周期
if (typeof onHide === 'function') onHide(stop)
if (typeof onUnload === 'function') onUnload(stop)
2025-12-06 14:18:33 +08:00
return {
speak,
pause,
resume,
stop,
2025-12-07 17:06:20 +08:00
cancelAudio: stop,
2025-12-06 14:18:33 +08:00
isSpeaking,
isPaused,
isLoading
}
}
/**
2025-12-07 17:06:20 +08:00
* 提取文本逻辑 (保持不变)
2025-12-06 14:18:33 +08:00
*/
function extractSpeechText(markdown) {
if (!markdown || markdown.indexOf('job-json') === -1) {
return markdown;
}
const jobRegex = /``` job-json\s*({[\s\S]*?})\s*```/g;
const jobs = [];
let match;
let lastJobEndIndex = 0;
let firstJobStartIndex = -1;
while ((match = jobRegex.exec(markdown)) !== null) {
const jobStr = match[1];
try {
const job = JSON.parse(jobStr);
jobs.push(job);
if (firstJobStartIndex === -1) {
firstJobStartIndex = match.index;
}
lastJobEndIndex = jobRegex.lastIndex;
} catch (e) {
console.warn('JSON 解析失败', e);
}
}
const guideText = firstJobStartIndex > 0 ?
markdown.slice(0, firstJobStartIndex).trim() : '';
const endingText = lastJobEndIndex < markdown.length ?
markdown.slice(lastJobEndIndex).trim() : '';
const jobTexts = jobs.map((job, index) => {
return `${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${job.salary},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}`;
});
const finalTextParts = [];
if (guideText) finalTextParts.push(guideText);
finalTextParts.push(...jobTexts);
if (endingText) finalTextParts.push(endingText);
return finalTextParts.join('\n');
}