更语音静态资源
This commit is contained in:
parent
e16afaf4d6
commit
ce984a9f8b
@ -1,7 +1,9 @@
|
|||||||
.h1 {
|
.h1 {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
body{
|
||||||
|
background-color: transparent !important;
|
||||||
|
}
|
||||||
.voice-box {
|
.voice-box {
|
||||||
box-sizing : border-box;
|
box-sizing : border-box;
|
||||||
display : flex;
|
display : flex;
|
||||||
@ -9,7 +11,7 @@
|
|||||||
margin : 50px auto;
|
margin : 50px auto;
|
||||||
padding : 50px;
|
padding : 50px;
|
||||||
width : 60%;
|
width : 60%;
|
||||||
border : 1px solid gray;
|
|
||||||
border-radius : 3px;
|
border-radius : 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,16 +3,16 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<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 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" />
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||||
<title>迅飞语音听写(流式版)WebAPI</title>
|
|
||||||
<link rel="stylesheet" href="./css/base.css" />
|
<link rel="stylesheet" href="./css/base.css" />
|
||||||
<script async src="https://hm.baidu.com/hm.js?85fad12bb9a6dab448f4eff0a19299a5"></script>
|
<!-- <script async src="https://hm.baidu.com/hm.js?85fad12bb9a6dab448f4eff0a19299a5"></script> -->
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body style="background-color: transparent;">
|
<body>
|
||||||
<h1 class="h1">迅飞语音听写(流式版)WebAPI</h1>
|
|
||||||
<hr>
|
|
||||||
<section class="voice-box">
|
<section class="voice-box">
|
||||||
<input type="search" name="voice" id="voice-txt" />
|
<input type="search" name="voice" id="voice-txt" />
|
||||||
<button id="start-btn">开始识别</button>
|
<button id="start-btn">开始识别</button>
|
||||||
@ -27,7 +27,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js">
|
||||||
|
</script>
|
||||||
<script src="./js/crypto-js.min.js"></script>
|
<script src="./js/crypto-js.min.js"></script>
|
||||||
<script src="./js/voice.js"></script>
|
<script src="./js/voice.js"></script>
|
||||||
|
|
||||||
@ -40,6 +41,7 @@
|
|||||||
const closeBtn = document.querySelector('#close-btn');
|
const closeBtn = document.querySelector('#close-btn');
|
||||||
let times = null;
|
let times = null;
|
||||||
|
|
||||||
|
|
||||||
// 实例化迅飞语音听写(流式版)WebAPI
|
// 实例化迅飞语音听写(流式版)WebAPI
|
||||||
const voice = new Voice({
|
const voice = new Voice({
|
||||||
|
|
||||||
@ -57,18 +59,36 @@
|
|||||||
//监听识别结果的变化
|
//监听识别结果的变化
|
||||||
voiceTxt.value = text;
|
voiceTxt.value = text;
|
||||||
fixedTxt.innerText = text;
|
fixedTxt.innerText = text;
|
||||||
|
console.log(text,'11111')
|
||||||
// 3秒钟内没有说话,就自动关闭
|
// 3秒钟内没有说话,就自动关闭
|
||||||
if (text) {
|
if (text) {
|
||||||
clearTimeout(times);
|
setTimeout(() => {
|
||||||
times = setTimeout(() => {
|
voice.stop();
|
||||||
this.stop(); // voice.stop();
|
uni.postMessage({
|
||||||
|
data: text
|
||||||
|
});
|
||||||
fixedBox.style.display = 'none';
|
fixedBox.style.display = 'none';
|
||||||
}, 3000);
|
}, 0);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
addUniEvenPassthrough()
|
||||||
|
addcloEvenPassthrough()
|
||||||
|
|
||||||
|
function addUniEvenPassthrough() {
|
||||||
|
window.uniEvent = function(info) {
|
||||||
|
console.log('11111')
|
||||||
|
voice.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function addcloEvenPassthrough() {
|
||||||
|
window.closeEvent = function(info) {
|
||||||
|
console.log('2222')
|
||||||
|
voice.stop();
|
||||||
|
fixedBox.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
// 开始识别
|
// 开始识别
|
||||||
startBtn['onclick'] = function() {
|
startBtn['onclick'] = function() {
|
||||||
voice.start();
|
voice.start();
|
||||||
|
@ -74,6 +74,7 @@
|
|||||||
} else {
|
} else {
|
||||||
self.webWorker = new Worker('./js/transcode.worker.js');
|
self.webWorker = new Worker('./js/transcode.worker.js');
|
||||||
self.webWorker.onmessage = function (event) {
|
self.webWorker.onmessage = function (event) {
|
||||||
|
|
||||||
self.audioData.push(...event.data);
|
self.audioData.push(...event.data);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -124,6 +125,7 @@
|
|||||||
this.setStatus('init');
|
this.setStatus('init');
|
||||||
iatWS.onopen = e => {
|
iatWS.onopen = e => {
|
||||||
this.setStatus('ing');
|
this.setStatus('ing');
|
||||||
|
console.log('连接成功')
|
||||||
// 重新开始录音
|
// 重新开始录音
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.webSocketSend();
|
this.webSocketSend();
|
||||||
@ -217,6 +219,7 @@
|
|||||||
// 向webSocket发送数据(音频二进制数据经过Base64处理)
|
// 向webSocket发送数据(音频二进制数据经过Base64处理)
|
||||||
webSocketSend() {
|
webSocketSend() {
|
||||||
if (this.webSocket.readyState !== 1) return false;
|
if (this.webSocket.readyState !== 1) return false;
|
||||||
|
|
||||||
// 音频数据
|
// 音频数据
|
||||||
const audioData = this.audioData.splice(0, 1280);
|
const audioData = this.audioData.splice(0, 1280);
|
||||||
const params = {
|
const params = {
|
||||||
@ -263,6 +266,7 @@
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
console.log(this.audioData)
|
||||||
// 中间帧
|
// 中间帧
|
||||||
this.webSocket.send(
|
this.webSocket.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@ -278,7 +282,9 @@
|
|||||||
};
|
};
|
||||||
// 识别结束 webSocket返回数据
|
// 识别结束 webSocket返回数据
|
||||||
webSocketRes(resultData) {
|
webSocketRes(resultData) {
|
||||||
|
|
||||||
let jsonData = JSON.parse(resultData);
|
let jsonData = JSON.parse(resultData);
|
||||||
|
console.log(JSON.stringify( jsonData))
|
||||||
if (jsonData.data && jsonData.data.result) {
|
if (jsonData.data && jsonData.data.result) {
|
||||||
let data = jsonData.data.result;
|
let data = jsonData.data.result;
|
||||||
let str = '';
|
let str = '';
|
||||||
@ -307,13 +313,16 @@
|
|||||||
}
|
}
|
||||||
if (jsonData.code === 0 && jsonData.data.status === 2) {
|
if (jsonData.code === 0 && jsonData.data.status === 2) {
|
||||||
this.webSocket.close();
|
this.webSocket.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
if (jsonData.code !== 0) {
|
if (jsonData.code !== 0) {
|
||||||
this.webSocket.close();
|
this.webSocket.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// 启动录音
|
// 启动录音
|
||||||
recorderStart() {
|
recorderStart() {
|
||||||
|
|
||||||
if (!this.audioContext) {
|
if (!this.audioContext) {
|
||||||
this.recorderInit();
|
this.recorderInit();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user