这是我在javascript中进行语音识别的代码吗?我想在表格的输入字段中获得结果的值。结果将在r.innerhtml中出现。 我怎么能这样做?
我在下面分享代码。
<form>
<input style="font-size: 14px;" id="result">
</form>
<button onmousedown="startConverting()" onmouseup="stopConverting()"><i class="fa fa-microphone"></i></button>
<script type="text/javascript">
var r = document.getElementById('result');
var speechRecognizer = new webkitSpeechRecognition();
function startConverting(){
if('webkitSpeechRecognition' in window){
speechRecognizer.continuous = true;
speechRecognizer.interimResults = true;
speechRecognizer.lang = 'en-IN';
speechRecognizer.start();
var finalTranscripts = '';
speechRecognizer.onresult = function(event){
var interimTranscripts = '';
for(var i = event.resultIndex; i < event.results.length; i++){
var transcript = event.results[i][0].transcript;
transcript.replace("\\n", "<br>");
if(event.results[i].isFinal){
finalTranscripts += transcript;
}else{
interimTranscripts += transcript;
}
}
r.innerText = finalTranscripts + '<span style="color:#999">' + interimTranscripts + '</span>';
};
speechRecognizer.onerror = function (event) {
};
}else{
r.innerText = 'Your browser is not supported. If google chrome, please upgrade!';
}
}
function stopConverting()
{
speechRecognizer.stop();
}
</script>