企业信息补全页面开发
This commit is contained in:
@@ -149,7 +149,19 @@ export function useAudioRecorder() {
|
||||
|
||||
const startRecording = async () => {
|
||||
if (isRecording.value) return
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
$api.msg('小程序暂不支持语音识别功能');
|
||||
return;
|
||||
// #endif
|
||||
|
||||
// #ifdef H5
|
||||
try {
|
||||
if (typeof navigator === 'undefined' || !navigator.mediaDevices) {
|
||||
$api.msg('当前环境不支持录音功能');
|
||||
return;
|
||||
}
|
||||
|
||||
recognizedText.value = ''
|
||||
lastFinalText.value = ''
|
||||
await connectWebSocket()
|
||||
@@ -191,6 +203,7 @@ export function useAudioRecorder() {
|
||||
console.error('启动失败:', err)
|
||||
cleanup()
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
const stopRecording = () => {
|
||||
|
@@ -15,8 +15,17 @@ export function useTTSPlayer(wsUrl) {
|
||||
const isPaused = ref(false)
|
||||
const isComplete = ref(false)
|
||||
|
||||
const audioContext = new(window.AudioContext || window.webkitAudioContext)()
|
||||
let playTime = audioContext.currentTime
|
||||
// #ifdef H5
|
||||
const audioContext = typeof window !== 'undefined' && (window.AudioContext || window.webkitAudioContext)
|
||||
? new(window.AudioContext || window.webkitAudioContext)()
|
||||
: null
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
const audioContext = null // 微信小程序不支持 AudioContext
|
||||
// #endif
|
||||
|
||||
let playTime = audioContext ? audioContext.currentTime : 0
|
||||
let sourceNodes = []
|
||||
let socket = null
|
||||
let sampleRate = 16000
|
||||
@@ -28,6 +37,11 @@ export function useTTSPlayer(wsUrl) {
|
||||
let activePlayId = 0
|
||||
|
||||
const speak = (text) => {
|
||||
if (!audioContext) {
|
||||
console.warn('⚠️ TTS not supported in current environment');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🎤 TTS speak function called');
|
||||
console.log('📝 Text to synthesize:', text ? text.substring(0, 100) + '...' : 'No text');
|
||||
console.log('🔗 WebSocket URL:', wsUrl);
|
||||
@@ -44,6 +58,11 @@ export function useTTSPlayer(wsUrl) {
|
||||
}
|
||||
|
||||
const pause = () => {
|
||||
if (!audioContext) {
|
||||
console.warn('⚠️ TTS not supported in current environment');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('⏸️ TTS pause called');
|
||||
console.log('🔊 AudioContext state:', audioContext.state);
|
||||
console.log('🔊 Is speaking before pause:', isSpeaking.value);
|
||||
@@ -63,6 +82,11 @@ export function useTTSPlayer(wsUrl) {
|
||||
}
|
||||
|
||||
const resume = () => {
|
||||
if (!audioContext) {
|
||||
console.warn('⚠️ TTS not supported in current environment');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('▶️ TTS resume called');
|
||||
console.log('🔊 AudioContext state:', audioContext.state);
|
||||
console.log('🔊 Is speaking before resume:', isSpeaking.value);
|
||||
@@ -89,7 +113,7 @@ export function useTTSPlayer(wsUrl) {
|
||||
isSpeaking.value = false
|
||||
isPaused.value = false
|
||||
isComplete.value = false
|
||||
playTime = audioContext.currentTime
|
||||
playTime = audioContext ? audioContext.currentTime : 0
|
||||
|
||||
sourceNodes.forEach(node => {
|
||||
try {
|
||||
@@ -113,11 +137,16 @@ export function useTTSPlayer(wsUrl) {
|
||||
isSpeaking.value = false
|
||||
isPaused.value = false
|
||||
isComplete.value = false
|
||||
playTime = audioContext.currentTime
|
||||
playTime = audioContext ? audioContext.currentTime : 0
|
||||
initWebSocket()
|
||||
}
|
||||
|
||||
const initWebSocket = () => {
|
||||
if (!audioContext) {
|
||||
console.warn('⚠️ WebSocket TTS not supported in current environment');
|
||||
return;
|
||||
}
|
||||
|
||||
const thisPlayId = currentPlayId
|
||||
console.log('🔌 Initializing WebSocket connection');
|
||||
console.log('🔗 WebSocket URL:', wsUrl);
|
||||
@@ -167,7 +196,7 @@ export function useTTSPlayer(wsUrl) {
|
||||
console.log('✅ TTS synthesis completed');
|
||||
isComplete.value = true
|
||||
// 计算剩余播放时间,确保播放完整
|
||||
const remainingTime = Math.max(0, (playTime - audioContext.currentTime) * 1000);
|
||||
const remainingTime = audioContext ? Math.max(0, (playTime - audioContext.currentTime) * 1000) : 0;
|
||||
console.log('⏱️ Remaining play time:', remainingTime + 'ms');
|
||||
setTimeout(() => {
|
||||
if (thisPlayId === activePlayId) {
|
||||
@@ -205,6 +234,8 @@ export function useTTSPlayer(wsUrl) {
|
||||
}
|
||||
|
||||
const pcmToAudioBuffer = (pcm, sampleRate, numChannels) => {
|
||||
if (!audioContext) return null;
|
||||
|
||||
const length = pcm.length / numChannels
|
||||
const audioBuffer = audioContext.createBuffer(numChannels, length, sampleRate)
|
||||
for (let ch = 0; ch < numChannels; ch++) {
|
||||
@@ -218,6 +249,8 @@ export function useTTSPlayer(wsUrl) {
|
||||
}
|
||||
|
||||
const playBuffer = (audioBuffer) => {
|
||||
if (!audioContext || !audioBuffer) return;
|
||||
|
||||
console.log('🎵 playBuffer called, duration:', audioBuffer.duration + 's');
|
||||
if (!isSpeaking.value) {
|
||||
playTime = audioContext.currentTime
|
||||
@@ -259,7 +292,10 @@ export function useTTSPlayer(wsUrl) {
|
||||
onHide(cancelAudio)
|
||||
onUnload(cancelAudio)
|
||||
|
||||
initWebSocket()
|
||||
// 只在支持 AudioContext 的环境中初始化 WebSocket
|
||||
if (audioContext) {
|
||||
initWebSocket()
|
||||
}
|
||||
|
||||
return {
|
||||
speak,
|
||||
|
Reference in New Issue
Block a user