-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.html
90 lines (76 loc) · 3.62 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum=1.0, user-scalable=no shrink-to-fit=no" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>迅飞语音听写(流式版)WebAPI</title>
<link rel="stylesheet" href="./css/base.css" />
<script async src="https://hm.baidu.com/hm.js?85fad12bb9a6dab448f4eff0a19299a5"></script>
</head>
<body>
<h1 class="h1">迅飞语音听写(流式版)WebAPI</h1>
<hr>
<section class="voice-box">
<input type="search" name="voice" id="voice-txt" />
<button id="start-btn">开始识别</button>
</section>
<section class="fixed-box" id="fixed-box">
<div class="fixed-main">
<button class="fixed-close" id="close-btn"></button>
<div id="fixed-txt">Hello! 请说出你想说话。。。!</div>
<div class="fixed-icon">
<img src="./img/voice.png" alt="" />
</div>
</div>
</section>
<script src="./js/crypto-js.min.js"></script>
<script src="./js/xf-voice-dictation.js"></script>
<script>
window.onload = function () {
const voiceTxt = document.querySelector('#voice-txt');
const startBtn = document.querySelector('#start-btn');
const fixedBox = document.querySelector('#fixed-box');
const fixedTxt = document.querySelector('#fixed-txt');
const closeBtn = document.querySelector('#close-btn');
let times = null;
// 实例化迅飞语音听写(流式版)WebAPI
const xfVoice = new XfVoiceDictation({
// 服务接口认证信息 注:APISecret 和 APIKey 的长度都差不多,请要填错哦,!
APPID: '5ec244d5',
APISecret: '37912e3e3f205e2a6201ec290452470a',
APIKey: '78b6c006f1f3df5e24d315e3dff09212',
// 注:要获取以上3个参数,请到迅飞开放平台:https://www.xfyun.cn/services/voicedictation 【注:这是我的迅飞语音听写(流式版)每天服务量500(也就是调500次),如果你需求里大请购买服务量:https://www.xfyun.cn/services/voicedictation?target=price】
// webSocket请求地址 非必传参数,默认为:wss://iat-api.xfyun.cn/v2/iat
// url: '',
onWillStatusChange: function (oldStatus, newStatus) {
//可以在这里进行页面中一些交互逻辑处理:注:倒计时(语音听写只有60s),录音的动画,按钮交互等!
fixedBox.style.display = 'block';
},
onTextChange: function (text) {
//监听识别结果的变化
voiceTxt.value = text;
fixedTxt.innerText = text;
// 3秒钟内没有说话,就自动关闭
if (text) {
clearTimeout(times);
times = setTimeout(() => {
this.stop(); // xfVoice.stop();
fixedBox.style.display = 'none';
}, 3000);
};
}
});
// 开始识别
startBtn['onclick'] = function () {
xfVoice.start();
};
// 关闭识别
closeBtn['onclick'] = function () {
xfVoice.stop();
fixedBox.style.display = 'none';
};
};
</script>
</body>
</html>