-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
92 lines (83 loc) · 2.94 KB
/
index.html
File metadata and controls
92 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>语音转文字输入</title>
<script src="https://www.mashaojie.cn/script/active-key.min.js" charset="UTF-8"></script>
<script src="https://www.mashaojie.cn/script/speechrecognizer.js"></script>
<script src="https://www.mashaojie.cn/script/operation-ball.min.js" charset="UTF-8"></script>
</head>
<body>
<style>
/*如果是禁用长按选择文字功能,用css*/
button {
user-select: none;
-webkit-user-select: none;
}
</style>
<div style="width: 100%; height: 50%;">
<button id="start-btn">开始录音[web speech api版本,需要翻墙到美国才能使用]</button>
<div>web speech api识别结果:</div>
<div id="webSpeechApiResult"></div>
</div>
<br>
<div>---------------------------------------------------------------------------</div>
<div style="width: 100%; height: 50%;">
<div>腾讯语音识别结果:</div>
<div id="TXSpeechApiResult"></div>
</div>
<script>
// 如果是想禁用长按弹出菜单, 用js的话,阻止默认事件
window.addEventListener('contextmenu', function(e){
e.preventDefault();
});
const mySpeechRecognition = webkitSpeechRecognition || SpeechRecognition
const startBtn = document.querySelector('#start-btn');
const resultDiv = document.querySelector('#webSpeechApiResult');
let recognition
startBtn.addEventListener('click', () => {
if(!recognition) {
recognition = new mySpeechRecognition();
recognition.lang = 'zh-CN';
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = (event) => {
let finalTranscript = '';
let interimTranscript = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
const transcript = event.results[i][0].transcript;
if (event.results[i].isFinal) {
finalTranscript += transcript;
resultDiv.innerHTML = finalTranscript;
} else {
interimTranscript += transcript;
resultDiv.innerHTML = interimTranscript;
}
}
};
recognition.onerror = (event) => {
console.error("recognition onerror", event)
}
recognition.onstart = () => {
console.log("recognition start")
}
recognition.onend = () => {
console.log("recognition end")
resultDiv.innerHTML = '';
}
recognition.start();
} else {
recognition.stop();
recognition = null;
}
});
</script>
<script>
document.dispatchEvent(new CustomEvent('init-speech-ball'));
document.addEventListener('speech-comfirm', function (e) {
document.querySelector('#TXSpeechApiResult').innerHTML = e.detail.result
})
</script>
</body>
</html>