|
|
@@ -327,6 +327,17 @@ const toggleSpeechInput = async () => {
|
|
|
ElMessage.error('语音录制功能需要在安全上下文(HTTPS或localhost)中运行')
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ // 检查浏览器类型
|
|
|
+ const userAgent = window.navigator.userAgent.toLowerCase()
|
|
|
+ const isFirefox = userAgent.includes('firefox')
|
|
|
+ const isChrome = userAgent.includes('chrome') && !userAgent.includes('edg')
|
|
|
+ const isEdge = userAgent.includes('edg')
|
|
|
+
|
|
|
+ // 提示浏览器兼容性
|
|
|
+ if (!isChrome && !isEdge) {
|
|
|
+ ElMessage.warning('当前浏览器可能与语音识别功能存在兼容性问题,可能会影响识别准确率')
|
|
|
+ }
|
|
|
|
|
|
// 无论当前状态如何,先清除可能存在的旧定时器
|
|
|
clearInterval(countdownTimer.value)
|
|
|
@@ -386,11 +397,23 @@ const toggleSpeechInput = async () => {
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
|
|
mediaStream.value = stream
|
|
|
|
|
|
+ // 检查浏览器类型
|
|
|
+ const userAgent = window.navigator.userAgent.toLowerCase()
|
|
|
+ const isFirefox = userAgent.includes('firefox')
|
|
|
+
|
|
|
// 使用Web Audio API捕获音频并转换为PCM格式
|
|
|
- // 不指定采样率,使用默认值,确保与媒体流兼容
|
|
|
- audioContext.value = new (window.AudioContext || window.webkitAudioContext)()
|
|
|
+ // 优先尝试使用16000Hz采样率,火狐浏览器使用默认值
|
|
|
+ let audioContextOptions = {}
|
|
|
+ if (!isFirefox) {
|
|
|
+ audioContextOptions = { sampleRate: 16000 }
|
|
|
+ }
|
|
|
+
|
|
|
+ audioContext.value = new (window.AudioContext || window.webkitAudioContext)(audioContextOptions)
|
|
|
mediaStreamSource.value = audioContext.value.createMediaStreamSource(stream)
|
|
|
|
|
|
+ // 检查实际采样率
|
|
|
+ console.log('AudioContext采样率:', audioContext.value.sampleRate)
|
|
|
+
|
|
|
// 尝试使用AudioWorkletNode(现代浏览器推荐),如果不支持则回退到ScriptProcessorNode
|
|
|
if (audioContext.value.audioWorklet && window.AudioWorkletNode) {
|
|
|
try {
|