本文介绍了打字文本中的语音识别与语音合成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过如下所示创建界面,在打字脚本中运行SpeechRecognition,运行正常:
namespace CORE{
export interface IWindow extends Window{
webkitSpeechRecognition: any;
}
}
我尝试使用同样的方法来处理SpeechSynsing,但FIELD,并且以下代码不起作用:
namespace CORE{
export interface IWindow extends Window{
SpeechSynthesisUtterance: any;
speechSynthesis: any;
}
}
我的问题是:
- 是我用来定义
SpeechRecognition
的最好方式练习时要遵循打字稿,否则会有更好的方法。- 如何在打印脚本中使用
SpeechSynthesis
。
- 如何在打印脚本中使用
作为参考,以下是我对SpeechRecognition
的工作代码:
namespace CORE{
export interface IWindow extends Window{
webkitSpeechRecognition: any;
}
}
namespace CORE{
export class speakRecognation{
// spoken:string;
constructor(input:HTMLInputElement){
var audio = new Audio('/sounds/sound.mp3');
//Voice recognition
const {webkitSpeechRecognition}: IWindow = <IWindow>window;
const recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = true;
input.addEventListener("click", function(){
audio.play();
recognition.start();
});
recognition.onstart = function () {
recognition.recognizing = true;
};
recognition.onresult = function (event) {
var interim_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
var result = event.results[i][0].transcript;
input.value = result;
// input.disable=false;
Program.execute(result);
recognition.stop();
} else {
interim_transcript += event.results[i][0].transcript;
input.value = interim_transcript;
}
}
};
recognition.onerror = function (event) {
input.value = "Something went wrong. Try reloading the page.";
}
recognition.onnomatch = function (event) {
input.value = "no match";
}
input.addEventListener("blur", function(e) {
recognition.stop();
input.value='';
});
input.addEventListener('keypress', function (e) {
recognition.stop();
var key = e.which || e.keyCode;
if (key === 13) { // 13 is enter
Program.execute(input.value);
}
});
}
}
}
和低于我的试用SpeachSynthesis
;
namespace CORE{
export interface IWindow extends Window{
SpeechSynthesisUtterance: any;
SpeechSynthesis: any;
}
}
namespace CORE{
export class speakSynthesis{
constructor(input:String){
if ('speechSynthesis' in window) {
console.log('Your browser supports speech synthesis.');
// speak('hi');
} else {
alert('Sorry your browser does not support speech synthesis. Try this in <a href="https://www.google.com/chrome/browser/desktop/index.html">Google Chrome</a>.');
}
const {SpeechSynthesisUtterance}: IWindow = <IWindow>window;
const {SpeechSynthesis}: IWindow = <IWindow>window;
// Create a new instance of SpeechSynthesisUtterance.
var msg = new SpeechSynthesisUtterance();
// Set the text.
msg.text = input;
// Set the attributes.
msg.lang = 'en-US';
// msg.voice = 'native'; msg.voice = 'Google US English'; // 'Google UK English Female'
msg.voice = 'Google US English'
msg.volume = 1;
msg.rate = 1;
msg.pitch = 1;
// msg.onend = function(event) { console.log('Speech complete'); }
// Queue this utterance.
var talk = new SpeechSynthesis();
window.talk.speak(msg);
}
}
}
推荐答案
多亏了this,我找到了将speechSynthesis
添加到Window
变量的解决方案:
(<any>window).speechSynthesis.speak(msg);
// OR
(window as any).talk.speak(msg);
此外,我在代码中发现了另一个错误,即:speechSynthesis
应该以小写s
开头,我的错误是大写S
我喜欢把答案贴出来,以防将来有人需要它。
这篇关于打字文本中的语音识别与语音合成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!