Home  >  Article  >  Web Front-end  >  A brief analysis of how to use JavaScript for speech recognition

A brief analysis of how to use JavaScript for speech recognition

高洛峰
高洛峰Original
2016-12-08 16:13:443235browse

1. Basic Usage

var recognition = new webkitSpeechRecognition();
recognition.onresult = function(event) {
 console.log(event)
}
recognition.start();

The operation here will actually enable the user authorization page to turn on the microphone. If the user allows it, the user can start talking. If you stop talking, the onresult registration time will be triggered, and Will return the captured audio as a JavaScript object.

2. Response flow

You need to wait for the user to be ready for the conversation and know that the conversation is over;

var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function(event) {
 console.log(event)
}
recognition.start();

A brief analysis of how to use JavaScript for speech recognition

This way you can render the result in advance when the user starts to speak.

You can automatically select the recognized language, and the default is the local language.

Three, x-webkit-speech

Webkit-based browser supports voice input

<input type="text" x-webkit-speech />

It will recognize audio and convert it into text

Four. Security

Browser under http protocol The user will be reminded to confirm the voice operation every time, but the https page does not have such a troublesome operation.
JavaScript context, the entire page, can access the captured audio.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn