我正在尝试使用 Web Speech API 创建一个 Web 应用程序,用户在开始时单击一个按钮来开始语音录制。我试图让它在用户停止说话时自动停止,然后我想使用谷歌翻译 API 来翻译用户的语音。我已经包含了我遇到问题的 Web Speech 部分的代码。
if (window.webkitSpeechRecognition) {
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = false;
recognition.onresult = function(event) {
var i;
return $('#transcript').text($('#transcript').text() + ((function() {
var _i, _ref, _ref1, _results;
_results = [];
for (i = _i = _ref = event.resultIndex, _ref1 = event.results.length - 1; _i <= _ref1; i = _i += 1) {
_results.push(event.results[i][0].transcript);
}
return _results;
})()).join(''));
};
$('#startStopButton').on('click', function() {
if (this.innerText === 'Start') {
this.innerText = 'Stop';
recognition.lang = 'en-AU';
return recognition.speechstart();
} else {
this.innerText = 'Start';
return recognition.speechend();
}
});
} else {
alert('Cannot access the speech recognition API. Are you using Chrome 25+ ?');
}