我使用Web Speech API在我的網頁上捕獲語音命令,但識別器在一段時間後結束(它會停止偵聽並觸發最後一個事件)。Chrome的webkitSpeechRecognition隨機結束
爲什麼會發生這種情況?我可以預防它嗎?
這是所有需要聲音識別頁面(40行)並重現錯誤的代碼。當識別器停止收聽時,它會提醒「結束」。
<h1>Voice Recognizer</h1>
<script>
if (!('webkitSpeechRecognition' in window)) {
alert('Your browser does not support speech recognition.');
} else {
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
console.log('started');
}
recognition.onresult = function() {
interim_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
console.log('interim result:', interim_transcript);
console.log('final reuslt:', final_transcript);
}
recognition.onerror = function() { alert('error'); }
recognition.onend = function() { alert('end'); }
function startListening(e){
final_transcript = '';
recognition.start();
}
startListening();
}
</script>