我正在尝试让 SpeechRecognition 在 Firefox 中工作,并了解这是截至 2019 年 12 月上个月的问题。
这是我从基本用法开始的代码
这是我在 Typescript 中的实现:
/**
* Start voice recognition.
*
* @param force (optional) force restart of listener service
*/
public async startVoiceRecognition(force?: boolean): Promise<void> {
console.log('Speech Recognition: ', window.SpeechRecognition, window.webkitSpeechRecognition)
// **START New Code following example in link above**
window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
if ((window.webkitSpeechRecognition) && this.fSpeechRecognitionRunning) {
// speech recognition API supported
console.log('API Supported');
this.recognition = new window.SpeechRecognition();
} else {
// speech recognition API not supported
console.log('API Not Supported');
return;
}
// **END New Code following example in link above**
// **Original Code**
// Check if Speech Recognition is available in this browser
**if (!(window.webkitSpeechRecognition) && !this.fSpeechRecognitionRunning) {
// console.log('Error: Speech not supported');
return;
}**
}
我不断得到的是:
错误错误:“未捕获(承诺):TypeError:webkitSpeechRecognition不是构造函数
我知道它在粗体行中,因为当我只用 SpeechRecognition 换出 webkitSpeechRecognition 时,我仍然会在不同的项目中遇到相同的错误......
错误错误:“未捕获(承诺):TypeError:SpeechRecognition不是构造函数
最后,这是我的 polyfill.ts ......
declare global {
interface Window {
AudioContext: typeof window.AudioContext;
webkitAudioContext: typeof window.AudioContext;
webkitSpeechRecognition: typeof window.SpeechRecognition;
}
}