Skip to content

Commit

Permalink
修复浏览器语音识别
Browse files Browse the repository at this point in the history
  • Loading branch information
xy3xy3 committed Nov 23, 2023
1 parent a5e49d5 commit ec1f021
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
startRecording() {
this.cur = 'start';
if (this.type == 1) {
this.recognitionText = "";

this.recognition.start();
} else {
this.rec.start();
Expand All @@ -158,6 +158,7 @@
let formData = new FormData();
formData.append('text', vm.recognitionText); // 提交用户选择的音色
formData.append('voice', vm.voice); // 提交用户选择的音色
vm.recognitionText = "";
$.ajax({
url: '/chat.php',
type: 'POST',
Expand Down Expand Up @@ -188,7 +189,6 @@
},
dataType: 'json' // Specify the expected data type as JSON
});
vm.recognitionText = "";
} else {
this.rec.stop(function(blob, duration) {
vm.status = 'load';
Expand Down Expand Up @@ -230,6 +230,7 @@
},
},
mounted() {
vm = this
if (localStorage.getItem('userConfig')) {
const userConfig = JSON.parse(localStorage.getItem('userConfig'));
this.type = userConfig.type || this.type;
Expand Down Expand Up @@ -260,17 +261,13 @@

// 在识别到结果时触发
this.recognition.onresult = function(event) {
let interimTranscript = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
// 如果是最终结果,将其添加到 recognitionText 中
vm.recognitionText += event.results[i][0].transcript;
} else {
// 如果是中间结果,将其添加到 interimTranscript 中
interimTranscript += event.results[i][0].transcript;
vm.recognitionText += event.results[i][0].transcript;
}
}
// 如果你需要处理中间结果,可以在这里对 interimTranscript 进行操作
};
} else {
alert('浏览器不支持语音识别功能');
Expand Down

0 comments on commit ec1f021

Please sign in to comment.