我正在为一个项目使用HTML5 voice API。我在尝试设置结果数组时遇到问题:event.results
。
我在听“A-H”变成“A-H”GO当我听到"GO“时,我想要重置events.result数组。我必须再次侦听相同的输入,但是第一个输入不能从event.results数组中删除。
我试过了:
event.results = [];
event.results.length = 0;
这些都不起作用。我也查过API,但我找不到我要做的事情的解决方案。
与我要做的事情类似的一个例子是,如果它正在侦听任何单词序列。但当它听到“取消”这个词时,它会忘记刚刚听到的一切。
我希望这是有意义的,任何帮助都是非常感谢的。
发布于 2014-12-10 23:52:53
结果似乎保存在一个SpeechRecognitionList
中,但我找不到任何用于操作列表本身的api。起作用的是停止并重新启动识别,但如果你不使用https-site (在一个普通的http站点上,浏览器在重新启动识别时总是要求许可),这就有问题了。
下面是一个例子:
<html>
<head>
<title>voice api test</title>
</head>
<body>
<h1>voice api test</h1>
<main></main>
<script>
(function startRecognizing() {
var main = document.querySelector('main');
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.addEventListener('result', function (event) {
var lastResult = event.results[event.results.length - 1];
if (lastResult[0].transcript.indexOf('cancel') > -1) {
recognition.stop();
while (main.children.length) main.removeChild(main.children[0]);
startRecognizing();
} else {
var p = document.createElement('p');
p.appendChild(document.createTextNode(lastResult[0].transcript));
main.appendChild(p);
}
});
recognition.start();
}());
</script>
</body>
</html>
https://stackoverflow.com/questions/27402001
复制相似问题