我想在Framer(coffeescript)中构建一个函数,在那里我可以说一个特定的单词作为语音输入而且会发生一些事情。例如,我说“苹果”然后一个矩形变成绿色。如果我说“香蕉”它会变黄。
到目前为止它只工作一次然后我必须刷新原型才能记录一个新单词。我怎样才能使它流利,所以我可以说更多的话,总是改变颜色?
这是我目前的代码。我使用Google Chrome API进行语音识别,因此原型本身只有在Chrome浏览器中打开时才有效。
SpeechRecognition = window.SpeechRecognition or
window.webkitSpeechRecognition
recognizer = new SpeechRecognition
recognizer.lang = 'de-DE'
recognizer.continuous = true
recognizer.interimResults = true
recognizer.start()
recognizer.onresult = (event) ->
result = event.results[event.resultIndex]
if result[0].transcript is "Apfel"
rect.backgroundColor = "green"
else if result[0].transcript is "Banane"
rect.backgroundColor = "yellow"
return
答案 0 :(得分:0)
使用带前缀的属性webkitSpeechRecognition()
和webkitSpeechGrammarList()
。只能让引擎识别"red"
作为输入语法,尽管result
事件不止一次被触发。
var grammar = '#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;'
var recognition = new webkitSpeechRecognition();
var speechRecognitionList = new webkitSpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.continuous = true;
recognition.lang = 'en-US';
recognition.interimResults = true;
recognition.maxAlternatives = 0;
var diagnostic = document.querySelector('.output');
var bg = document.querySelector('html');
document.body.onclick = function() {
recognition.start();
console.log('Ready to receive a color command.');
}
recognition.onresult = function(event) {
console.log(event);
var color = event.results[0][0].transcript;
diagnostic.textContent = 'Result received: ' + color;
bg.style.backgroundColor = color;
}
<body>
click
<div class="output">