開整

開整
之前我這邊微信小程序調(diào)用訊飛的接口還是發(fā)一段音頻到后臺 再去連接訊飛的websocket 真的 賊慢 要是兩三秒的還好 稍微長一點就GG
最近突然發(fā)現(xiàn)微信小程序有PCM格式了 所以就直接用小程序前臺websocket連接了
代碼在下面
訊飛賬號的申請我就不說了
首先是微信小程序 先得錄音 設(shè)置一下變量什么的
const app = getApp()
const recorderManager = wx.getRecorderManager();
var wxst; //語音websocket
var status = 0; // 音頻的狀態(tài)
var iatResult = [] // 識別結(jié)果
const searchoptions = {
duration: 60000,//指定錄音的時長,單位 ms
sampleRate: 8000,//采樣率
numberOfChannels: 1,//錄音通道數(shù)
encodeBitRate: 48000,//編碼碼率
format: 'PCM',//音頻格式
frameSize: 5,//指定幀大小,單位 KB
}
然后就是來個點擊事件 開啟咱們的錄音 獲取訊飛鑒權(quán)的是個接口 因為之前后臺寫過 就用了 同時也可以后臺記錄一下使用的次數(shù)什么的
java這邊的代碼
public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {
URL url = new URL(hostUrl);
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = format.format(new Date());
StringBuilder builder = new StringBuilder("host: ").append(url.getHost()).append("\n").//
append("date: ").append(date).append("\n").//
append("GET ").append(url.getPath()).append(" HTTP/1.1");
//System.out.println(builder);
Charset charset = Charset.forName("UTF-8");
Mac mac = Mac.getInstance("hmacsha256");
SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(charset), "hmacsha256");
mac.init(spec);
byte[] hexDigits = mac.doFinal(builder.toString().getBytes(charset));
String sha = Base64.getEncoder().encodeToString(hexDigits);
//System.out.println(sha);
String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha);
//System.out.println(authorization);
HttpUrl httpUrl = HttpUrl.parse("https://" + url.getHost() + url.getPath()).newBuilder().//
addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(charset))).//
addQueryParameter("date", date).//
addQueryParameter("host", url.getHost()).//
build();
return httpUrl.toString();
}
開始錄音的點擊事件
start_say: function (e) { //開始錄音按鈕
var that = this;
wx.getSetting({//查看用戶有沒有開啟語音權(quán)限
success(res) {
if (res.authSetting['scope.record']) {
wx.authorize({
scope: 'scope.record',
success() {
var xfurl = "";
wx.request({//請求接口 獲取訊飛語音鑒權(quán)
url: 接口地址,
method: "get",
header: {
'content-type': 'application/json' // 默認(rèn)值
},
success: function (res) {
if (res.data.code == "200" && res.data.data) {
xfurl = res.data.data;
wxst = wx.connectSocket({ // 開啟websocket連接
url: xfurl,
method: 'GET',
success: function (res) {
that.setData({//我這里是個遮罩層 開啟他
shows: true,
})
recorderManager.start(searchoptions);//開始錄音
}
});
} else {
wx.showToast({
title: '獲取語音鑒權(quán)失敗',
icon: 'none',
mask: true,
duration: 3000
})
}
},
fail: function () {
wx.showToast({
title: '獲取語音鑒權(quán)失敗',
icon: 'none',
mask: true,
duration: 3000
})
}
})
},
fail() {
wx.showModal({
title: '微信授權(quán)',
content: '您當(dāng)前未開啟語音權(quán)限,請在右上角設(shè)置(···)中開啟“錄音功能”',
showCancel: false,
success(res) {
if (res.confirm) {
console.log('用戶點擊確定')
}
}
})
}
})
}else{
wx.showModal({
title: '微信授權(quán)',
content: '您當(dāng)前未開啟語音權(quán)限,請在右上角設(shè)置(···)中開啟“錄音功能”',
showCancel: false,
success(res) {
if (res.confirm) {
console.log('用戶點擊確定')
}
}
})
}
}
})
}
關(guān)閉錄音的點擊事件
end_say: function () { //結(jié)束錄音按鈕
var that = this;
recorderManager.stop();
that.setData({//關(guān)閉遮罩層
shows: false,
})
}
之后就是關(guān)于錄音和websocket的監(jiān)聽 我這邊是先開啟websocket 之后 再去錄音
錄音的監(jiān)聽
onShow: function() {
var that = this;
recorderManager.onStart(() => {//開始錄音時觸發(fā)
status = 0;
iatResult = []
console.log('recorder start')
});
recorderManager.onError((res) => {//錯誤回調(diào)
console.log(res);
});
recorderManager.onStop((res) => {//結(jié)束錄音時觸發(fā)
console.log('recorder stop', res)
status = 2;
var sendsty = '{"data":{"status":2,"audio":"","format":"audio/L16;rate=8000","encoding":"raw"}}'
wxst.send({
data: sendsty
})
});
recorderManager.onFrameRecorded((res) => {//每幀觸發(fā)
const { frameBuffer } = res
var int16Arr = new Int8Array(res.frameBuffer);
const base64 = wx.arrayBufferToBase64(int16Arr)
switch (status) {
case 0:
status = 1;
var sendsty = '{"common":{"app_id":"訊飛的appid"},"business":{"language":"zh_cn","domain":"iat","accent":"mandarin","dwa":"wpgs","vad_eos":1000},"data":{"status":0,"format":"audio/L16;rate=8000","encoding":"raw","audio":"' + base64 + '"}}'
wxst.send({
data: sendsty
})
break;
case 1:
var sendsty = '{"data":{"status":1,"format":"audio/L16;rate=8000","encoding":"raw","audio":"' + base64 + '"}}'
wxst.send({
data: sendsty
})
break;
default:
console.log("default");
}
})
}
關(guān)于websocket的監(jiān)聽
onLoad: function(options) {
var that = this;
wx.onSocketOpen((res) => {// websocket打開
console.log('監(jiān)聽到 WebSocket 連接已打開' + res);
})
wx.onSocketError((err) => {//連接失敗
console.log('websocket連接失敗', err);
wx.showToast({
title: 'websocket連接失敗',
icon: 'none',
duration: 2000,
mask: false
})
})
wx.onSocketMessage((res) => {//接收返回值
var data = JSON.parse(res.data)
if (data.code != 0) {
console.log("error code " + data.code + ", reason " + data.message)
return
}
let str = ""
if (data.data.status == 2) {//最終識別結(jié)果
// data.data.status ==2 說明數(shù)據(jù)全部返回完畢,可以關(guān)閉連接,釋放資源
wxst.close();
} else {//中間識別結(jié)果
}
iatResult[data.data.result.sn] = data.data.result
if (data.data.result.pgs == 'rpl') {
data.data.result.rg.forEach(i => {
iatResult[i] = null
})
}
iatResult.forEach(i => {
if (i != null) {
i.ws.forEach(j => {
j.cw.forEach(k => {
str += k.w
})
})
}
})
that.setData({
searchKey: str //這個是中間的語音識別結(jié)果
})
})
wx.onSocketClose((res) => {//WebSocket連接已關(guān)閉!
var that = this;
recorderManager.stop();
that.setData({//把之前開開的遮罩層關(guān)上
shows: false,
})
var str = that.data.searchKey;
console.log(str);
str = str.replace(/\s*/g, "");//去除空格
if (str.substr(str.length - 1, 1) == "。") {//去除句號
str = str.substr(0, str.length - 1);
}
that.setData({
searchKey: str//這個是最后確定的語音識別結(jié)果
})
console.log('WebSocket連接已關(guān)閉!')
})
}
ok 大體上就是這樣 試了一下 一般說完話1秒左右會全部識別完畢 還是挺好了 這樣用戶體驗也不錯
第一次寫文章 有什么不好的地方大家一起交流 歡迎留言 奧力給

飲料雜貨鋪