更新记录
1.0.0(2021-06-03)
下载此版本
Hbuilderx 2.9.8
语音听写(流式版)WebAPI 文档
官方文档:https://www.xfyun.cn/doc/asr/voicedictation/API.html#%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 2.9.8 app-vue app-nvue |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
√ |
√ |
√ |
√ |
√ |
× |
√ |
√ |
√ |
该目录文件为讯飞官方语音听写(流式版)demo 中封装的音频采集类、接口方案。并做了修改。
index.vue 中的 "socketUrl" "appId" 需要用户在 科大讯飞网站获取 ;
使用时需要更换成自己的;否则websocket 链接会出错
index.vue
data() {
return {
showText: '',
receveText: '', //识别返回数据
resultTextTemp: '', // wpgs下的听写结果需要中间状态辅助记录
audioData: [], //音频数据
isTouch: false,
socketUrl: 'wss://iat-api.xfyun.cn/v2/iat?authorization=YXBpX2tleT0iM2JmMmIwOTYwODJhODEyNzQ1MjQ1Nzg5NTBlODU5YzIiLCBhbGdvcml0aG09ImhtYWMtc2hhMjU2IiwgaGVhZGVycz0iaG9zdCBkYXRlIHJlcXVlc3QtbGluZSIsIHNpZ25hdHVyZT0iL1JITUM0MWFReTZWRmhmSC8rTzBVSHBneE41M3c1a0FmMmtYN3V6UjBxQT0i&date=Thu, 03 Jun 2021 02:40:57 GMT&host=iat-api.xfyun.cn',
webSocket: null, //websocket 链接实例
isConnect: false,
status: 'null',
audioContext: null,
scriptProcessor: null,
transWorker: null,
audioTime: 60,
appId: '666666',
language: 'zh_cn',
accent: 'mandarin'
}
},
并且自行计算 获取;官方demo 中有详细说明:
官方 demo 链接:https://www.xfyun.cn/doc/asr/voicedictation/API.html#%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
const API_SECRET = 'xxx'
const API_KEY = 'xxxx'
function getWebSocketUrl() {
return new Promise((resolve, reject) => {
// 请求地址根据语种不同变化
var url = 'wss://iat-api.xfyun.cn/v2/iat'
var host = 'iat-api.xfyun.cn'
var apiKey = API_KEY
var apiSecret = API_SECRET
var date = new Date().toGMTString()
var algorithm = 'hmac-sha256'
var headers = 'host date request-line'
var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2/iat HTTP/1.1`
var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret)
var signature = CryptoJS.enc.Base64.stringify(signatureSha)
var authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`
var authorization = btoa(authorizationOrigin)
url = `${url}?authorization=${authorization}&date=${date}&host=${host}`
//url = `${url}?authorization=${authorization}&host=${host}`
//url = 'wss://iat-api.xfyun.cn/v2/iat?authorization=YXBpX2tleT0iM2JmMmIwOTYwODJhODEyNzQ1MjQ1Nzg5NTBlODU5YzIiLCBhbGdvcml0aG09ImhtYWMtc2hhMjU2IiwgaGVhZGVycz0iaG9zdCBkYXRlIHJlcXVlc3QtbGluZSIsIHNpZ25hdHVyZT0ibTd2bWJUODZNdzZvSnhuaUxVQlE3UU9ueXdRK1NobG4veXVUc0lNaHNQYz0i&date=Mon, 31 May 2021 09:28:56 GMT&host=iat-api.xfyun.cn'
console.log("url:",url)
resolve(url)
})
}