我正在尝试通过单击按钮进行搜索来开始语音识别,因为我使用的是此代码-
<style>
.speech {border: 1px solid #DDD; width: 300px; padding: 0; margin: 0}
.speech input {border: 0; width: 240px; display: inline-block; height: 30px;}
.speech img {float: right; width: 40px }
</style>
<!-- Search Form -->
<form id="labnol" method="get" action="https://www.google.com/search">
<div class="speech">
<input type="text" name="q" id="transcript" placeholder="Speak" />
<img onclick="startDictation()" src="//i.imgur.com/cHidSVu.gif" />
</div>
</form>
<!-- HTML5 Speech Recognition API -->
<script>
function startDictation() {
if (window.hasOwnProperty('webkitSpeechRecognition')) {
var recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.lang = "en-US";
recognition.start();
recognition.onresult = function(e) {
document.getElementById('transcript').value
= e.results[0][0].transcript;
recognition.stop();
document.getElementById('labnol').submit();
};
recognition.onerror = function(e) {
console.log(e);
recognition.stop();
}
}
}
</script>
我从recognition.onerror
-SpeechRecognitionError {isTrusted: true, error: "not-allowed", message: "", type: "error", target: SpeechRecognition, …}
我正在使用labnol
中的这段代码我无法弄清问题,为什么我会出错-not allowed
谁可以提供帮助。
答案 0 :(得分:1)
这是因为来源不受信任。
来源应至少与以下(方案,主机,端口)模式之一匹配:
(https, *, *)
(wss, *, *)
(*, localhost, *)
(*, 127/8, *)
(*, ::1/128, *)
(file, *, —)
(chrome-extension, *, —)
有关更多说明,请访问-https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features