实现实时语音识别转文字功能可以采用腾讯云语音识别接口 目前腾讯云语音识别有实时、语音、一句话等方案,我们采用实时语音方案,可以在腾讯云官网申请免费体验套餐进行测试
在控制台内可以看到使用记录以及右下角的帮助文档 按照文档方法调用api即可
1.在左侧秘钥处获取appid以及相关秘钥,保存在data内方便使用
data() {
return {
secretId:'',
appId:,
secretKey:'',
url:'asr.cloud.tencent.com/asr/v2/',
socket: null,
};
2.按照文档要求拼装参数,参数含义可以文档查看,同时对SecretKey使用 HmacSha1 算法进行加密并做 base64 编码处理
opens(){
const timestamp = parseInt(new Date().getTime() / 1000) - 1;
const params = {
secretid: this.secretId,
timestamp: timestamp,
expired: timestamp + 60 * 60,
nonce: timestamp,
engine_model_type: '16k_zh',
voice_id: timestamp.toString(),
voice_format: 8,
convert_num_mode:1,
};
// 处理参数为url
const url=this.getUrl(this.url,params)
this.connect(url)
},
getUrl(url,params){
let newurl=url+this.appId+'?'+Object.keys(params)
.sort(function(a, b) {
return a.localeCompare(b);
})
.map((key)=>{
return encodeURIComponent(key)+'='
+encodeURIComponent(params[key])
})
.join('&')
// 使用 HmacSha1 算法进行加密并做 base64 编码处理
let baseurl=this.baseUrl(newurl)
newurl=newurl+'&'+'signature'+'='
+encodeURIComponent(baseurl)
return newurl
},
baseUrl(newurl){
const hash = CryptoJS.HmacSHA1(newurl, this.secretKey);
return uni.arrayBufferToBase64(this.toUint8Array(hash));
},
toUint8Array(wordArray){
// Shortcuts
const words = wordArray.words;
const sigBytes = wordArray.sigBytes;
// Convert
const u8 = new Uint8Array(sigBytes);
for (let i = 0; i < sigBytes; i++) {
u8[i] = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
}
return u8;
},
参数名称 | 必填 | 类型 | 描述 |
---|---|---|---|
secretid | 是 | String | 腾讯云注册账号的密钥 SecretId,可通过 API 密钥管理页面 获取。 |
timestamp | 是 | Integer | 当前 UNIX 时间戳,单位为秒。如果与当前时间相差过大,会引起签名过期错误。 |
expired | 是 | Integer | 签名的有效期截止时间 UNIX 时间戳,单位为秒。expired 必须大于 timestamp 且 expired - timestamp 小于90天。 |
nonce | 是 | Integer | 随机正整数。用户需自行生成,最长10位。 |
engine_model_type | 是 | String | 引擎模型类型。 电话场景: • 8k_en:电话 8k 英语; • 8k_zh:电话 8k 中文普通话通用; • 8k_zh_finance:电话 8k 金融领域模型; 非电话场景: • 16k_zh:16k 中文普通话通用; • 16k_en:16k 英语; • 16k_ca:16k 粤语; • 16k_ko:16k 韩语; • 16k_zh-TW:16k 中文普通话繁体; • 16k_ja:16k 日语; • 16k_wuu-SH:16k 上海话方言; • 16k_zh_medical 医疗; • 16k_en_game 英文游戏; • 16k_zh_court 法庭; • 16k_en_edu 英文教育; • 16k_zh_edu 中文教育; • 16k_th 泰语。 |
voice_id | 是 | String | 16位 String 串作为每个音频的唯一标识,用户自己生成。 |
voice_format | 否 | Integer | 语音编码方式,可选,默认值为4。1:pcm;4:speex(sp);6:silk;8:mp3;12:wav;14:m4a(每个分片须是一个完整的 m4a 音频);16:aac。 |
needvad | 否 | Integer | 0:关闭 vad,1:开启 vad。 如果语音分片长度超过60秒,用户需开启 vad。 |
hotword_id | 否 | String | 热词 id。用于调用对应的热词表,如果在调用语音识别服务时,不进行单独的热词 id 设置,自动生效默认热词;如果进行了单独的热词 id 设置,那么将生效单独设置的热词 id。 |
customization_id | 否 | String | 自学习模型 id。用于调用对应的自学习模型,如果在调用语音识别服务时,不进行单独的自学习模型 id 设置,自动生效默认自学习模型;如果进行了单独的自学习模型 id 设置,那么将生效单独设置的自学习模型 id。 |
filter_dirty | 否 | Integer | 是否过滤脏词(目前支持中文普通话引擎)。默认为0。0:不过滤脏词;1:过滤脏词;2:将脏词替换为 * 。 |
filter_modal | 否 | Integer | 是否过语气词(目前支持中文普通话引擎)。默认为0。0:不过滤语气词;1:部分过滤;2:严格过滤 。 |
filter_punc | 否 | Integer | 是否过滤句末的句号(目前支持中文普通话引擎)。默认为0。0:不过滤句末的句号;1:过滤句末的句号。 |
convert_num_mode | 否 | Integer | 是否进行阿拉伯数字智能转换(目前支持中文普通话引擎)。0:不转换,直接输出中文数字,1:根据场景智能转换为阿拉伯数字,3: 打开数学相关数字转换。默认值为1。 |
word_info | 否 | Int | 是否显示词级别时间戳。0:不显示;1:显示,不包含标点时间戳,2:显示,包含标点时间戳。支持引擎 8k_en、8k_zh、8k_zh_finance、16k_zh、16k_en、16k_ca、16k_zh-TW、16k_ja、16k_wuu-SH,默认为0。 |
vad_silence_time | 否 | Integer | 语音断句检测阈值,静音时长超过该阈值会被认为断句(多用在智能客服场景,需配合 needvad = 1 使用),取值范围:240-2000,单位 ms,此参数建议不要随意调整,可能会影响识别效果,目前仅支持 8k_zh、8k_zh_finance、16k_zh 引擎模型。 |
signature | 是 | String | 接口签名参数。 |
3.使用socket进行链接
connect(url){
console.log(url)
this.socket=uni.connectSocket({
url: 'wss://'+url,
success: data => {
console.log('socket connect result ', data);
},
fail: e => {
this.loading(JSON.stringify(e));
}
});
this.socket.onOpen(function (res) {
console.log('WebSocket连接已打开!');
});
this.socket.onError(function (res) {
console.log('WebSocket连接打开失败,请检查!');
});
this.socket.onMessage((res)=>{
console.log(res.data)
res.data=JSON.parse(res.data)
console.log('收到服务器内容:' +res.data);
console.log(res.data)
if(res.data.code===0 && res.data.result){
console.log('识别成功',res.data.result)
}else if(res.data.code == 0){
this.startRecorder();
}else{
console.log('识别失败',res.data)
}
})
this.socket.onClose( (res)=> {
console.log('WebSocket 已关闭!');
this.socket = null;
this.stopRecorder();
});
},
closes(){
this.socket.close();
},
4.打开录音机
startRecorder(){
console.log('打开录音机')
if (this.recorder == null) {
const recorder = (this.recorder = uni.getRecorderManager());
recorder.onFrameRecorded(({ isLastFrame, frameBuffer }) => {
console.log(frameBuffer)
if (this.socket) {
this.socket.send({
data: frameBuffer
});
if (isLastFrame) {
this.socket.send({
data: JSON.stringify({
type: 'end'
})
});
}
}
});
recorder.onError(({ errMsg }) => {
console.log('recorder error', errMsg);
if (errMsg != "operateRecorder:fail:audio is stop, don't stop record again") {
this.loading('启动失败:' + errMsg);
// this.stopConnect();
}
});
recorder.onStart(() => {
console.log('recorder start');
//this.loading('正在识别');
this.startTimer();
});
recorder.onStop(({ tempFilePath }) => {
console.log('recorder stop', tempFilePath);
});
recorder.onPause(e => {
console.log('recorder pause');
});
}
this.recorder.start({
duration: 60 * 1000,
format: 'mp3',
frameSize: 1.25,
sampleRate: 16000,
numberOfChannels: 1
});
},
5.全部demo代码
<template>
<view>
<button @click="opens">打开</button>
<button @click="closes">关闭</button>
</view>
</template>
<script>
import CryptoJS from './cryptojs.js'
export default {
data() {
return {
secretId:'',
appId:,
secretKey:'',
url:'asr.cloud.tencent.com/asr/v2/',
socket: null,
recorder:null,
duration: 0,
durationTimer: null,
};
},
methods: {
opens(){
const timestamp = parseInt(new Date().getTime() / 1000) - 1;
const params = {
secretid: this.secretId,
timestamp: timestamp,
expired: timestamp + 60 * 60,
nonce: timestamp,
engine_model_type: '16k_zh',
voice_id: timestamp.toString(),
voice_format: 8,
convert_num_mode:1,
};
const url=this.getUrl(this.url,params)
this.connect(url)
},
getUrl(url,params){
let newurl=url+this.appId+'?'+Object.keys(params)
.sort(function(a, b) {
return a.localeCompare(b);
})
.map((key)=>{
return encodeURIComponent(key)+'='+encodeURIComponent(params[key])
})
.join('&')
let baseurl=this.baseUrl(newurl)
newurl=newurl+'&'+'signature'+'='+encodeURIComponent(baseurl)
return newurl
},
baseUrl(newurl){
const hash = CryptoJS.HmacSHA1(newurl, this.secretKey);
return uni.arrayBufferToBase64(this.toUint8Array(hash));
},
toUint8Array(wordArray){
// Shortcuts
const words = wordArray.words;
const sigBytes = wordArray.sigBytes;
// Convert
const u8 = new Uint8Array(sigBytes);
for (let i = 0; i < sigBytes; i++) {
u8[i] = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
}
return u8;
},
connect(url){
console.log(url)
this.socket=uni.connectSocket({
url: 'wss://'+url,
success: data => {
console.log('socket connect result ', data);
},
fail: e => {
this.loading(JSON.stringify(e));
}
});
this.socket.onOpen(function (res) {
console.log('WebSocket连接已打开!');
});
this.socket.onError(function (res) {
console.log('WebSocket连接打开失败,请检查!');
});
this.socket.onMessage((res)=>{
console.log(res.data)
res.data=JSON.parse(res.data)
console.log('收到服务器内容:' +res.data);
console.log(res.data)
if(res.data.code===0 && res.data.result){
console.log('识别成功',res.data.result)
}else if(res.data.code == 0){
this.startRecorder();
}else{
console.log('识别失败',res.data)
}
})
this.socket.onClose( (res)=> {
console.log('WebSocket 已关闭!');
this.socket = null;
this.stopRecorder();
});
},
closes(){
this.socket.close();
},
startRecorder(){
console.log('打开录音机')
if (this.recorder == null) {
const recorder = (this.recorder = uni.getRecorderManager());
recorder.onFrameRecorded(({ isLastFrame, frameBuffer }) => {
console.log(frameBuffer)
if (this.socket) {
this.socket.send({
data: frameBuffer
});
if (isLastFrame) {
this.socket.send({
data: JSON.stringify({
type: 'end'
})
});
}
}
});
recorder.onError(({ errMsg }) => {
console.log('recorder error', errMsg);
if (errMsg != "operateRecorder:fail:audio is stop, don't stop record again") {
this.loading('启动失败:' + errMsg);
// this.stopConnect();
}
});
recorder.onStart(() => {
console.log('recorder start');
//this.loading('正在识别');
this.startTimer();
});
recorder.onStop(({ tempFilePath }) => {
console.log('recorder stop', tempFilePath);
});
recorder.onPause(e => {
console.log('recorder pause');
});
}
this.recorder.start({
duration: 60 * 1000,
format: 'mp3',
frameSize: 1.25,
sampleRate: 16000,
numberOfChannels: 1
});
},
stopRecorder(){
if (this.recorder) {
this.recorder.stop()
}
},
startTimer: function() {
this.duration = 0;
this.durationTimer = setInterval(() => {
this.duration += 1;
}, 1000);
},
stopTimer: function() {
if (this.durationTimer) {
clearInterval(this.durationTimer);
this.durationTimer = null;
}
},
open: function() {
this.$refs.asr.show();
},
}
};
</script>
<style lang="scss"></style>
CryptoJS代码
/*
* [js-sha1]
*
* @version 0.6.0
* @copyright H, J-C 2018-9-28
* @license MIT
*/
var CryptoJS = function (g, l) {
var e = {}, d = e.lib = {}, m = function () { }, k = d.Base = {
extend: function (a) {
m.prototype = this;
var c = new m;
a && c.mixIn(a);
c.hasOwnProperty("init") || (c.init = function () {
c.$super.init.apply(this, arguments)
});
c.init.prototype = c;
c.$super = this;
return c
},
create: function () {
var a = this.extend();
a.init.apply(a, arguments);
return a
},
init: function () { },
mixIn: function (a) {
for (var c in a) a.hasOwnProperty(c) && (this[c] = a[c]);
a.hasOwnProperty("toString") && (this.toString = a.toString)
},
clone: function () {
return this.init.prototype.extend(this)
}
},
p = d.WordArray = k.extend({
init: function (a, c) {
a = this.words = a || [];
this.sigBytes = c != l ? c : 4 * a.length
},
toString: function (a) {
return (a || n).stringify(this)
},
concat: function (a) {
var c = this.words,
q = a.words,
f = this.sigBytes;
a = a.sigBytes;
this.clamp();
if (f % 4)
for (var b = 0; b < a; b++) c[f + b >>> 2] |= (q[b >>> 2] >>> 24 - 8 * (b % 4) & 255) << 24 - 8 * ((f + b) % 4);
else if (65535 < q.length)
for (b = 0; b < a; b += 4) c[f + b >>> 2] = q[b >>> 2];
else c.push.apply(c, q);
this.sigBytes += a;
return this
},
clamp: function () {
var a = this.words,
c = this.sigBytes;
a[c >>> 2] &= 4294967295 << 32 - 8 * (c % 4);
a.length = g.ceil(c / 4)
},
clone: function () {
var a = k.clone.call(this);
a.words = this.words.slice(0);
return a
},
random: function (a) {
for (var c = [], b = 0; b < a; b += 4) c.push(4294967296 * g.random() | 0);
return new p.init(c, a)
}
}),
b = e.enc = {}, n = b.Hex = {
stringify: function (a) {
var c = a.words;
a = a.sigBytes;
for (var b = [], f = 0; f < a; f++) {
var d = c[f >>> 2] >>> 24 - 8 * (f % 4) & 255;
b.push((d >>> 4).toString(16));
b.push((d & 15).toString(16))
}
return b.join("")
},
parse: function (a) {
for (var c = a.length, b = [], f = 0; f < c; f += 2) b[f >>> 3] |= parseInt(a.substr(f, 2), 16) << 24 - 4 * (f % 8);
return new p.init(b, c / 2)
}
}, j = b.Latin1 = {
stringify: function (a) {
var c = a.words;
a = a.sigBytes;
for (var b = [], f = 0; f < a; f++) b.push(String.fromCharCode(c[f >>> 2] >>> 24 - 8 * (f % 4) & 255));
return b.join("")
},
parse: function (a) {
for (var c = a.length, b = [], f = 0; f < c; f++) b[f >>> 2] |= (a.charCodeAt(f) & 255) << 24 - 8 * (f % 4);
return new p.init(b, c)
}
}, h = b.Utf8 = {
stringify: function (a) {
try {
return decodeURIComponent(escape(j.stringify(a)))
} catch (c) {
throw Error("Malformed UTF-8 data");
}
},
parse: function (a) {
return j.parse(unescape(encodeURIComponent(a)))
}
},
r = d.BufferedBlockAlgorithm = k.extend({
reset: function () {
this._data = new p.init;
this._nDataBytes = 0
},
_append: function (a) {
"string" == typeof a && (a = h.parse(a));
this._data.concat(a);
this._nDataBytes += a.sigBytes
},
_process: function (a) {
var c = this._data,
b = c.words,
f = c.sigBytes,
d = this.blockSize,
e = f / (4 * d),
e = a ? g.ceil(e) : g.max((e | 0) - this._minBufferSize, 0);
a = e * d;
f = g.min(4 * a, f);
if (a) {
for (var k = 0; k < a; k += d) this._doProcessBlock(b, k);
k = b.splice(0, a);
c.sigBytes -= f
}
return new p.init(k, f)
},
clone: function () {
var a = k.clone.call(this);
a._data = this._data.clone();
return a
},
_minBufferSize: 0
});
d.Hasher = r.extend({
cfg: k.extend(),
init: function (a) {
this.cfg = this.cfg.extend(a);
this.reset()
},
reset: function () {
r.reset.call(this);
this._doReset()
},
update: function (a) {
this._append(a);
this._process();
return this
},
finalize: function (a) {
a && this._append(a);
return this._doFinalize()
},
blockSize: 16,
_createHelper: function (a) {
return function (b, d) {
return (new a.init(d)).finalize(b)
}
},
_createHmacHelper: function (a) {
return function (b, d) {
return (new s.HMAC.init(a, d)).finalize(b)
}
}
});
var s = e.algo = {};
return e
}(Math);
(function () {
var g = CryptoJS,
l = g.lib,
e = l.WordArray,
d = l.Hasher,
m = [],
l = g.algo.SHA1 = d.extend({
_doReset: function () {
this._hash = new e.init([1732584193, 4023233417, 2562383102, 271733878, 3285377520])
},
_doProcessBlock: function (d, e) {
for (var b = this._hash.words, n = b[0], j = b[1], h = b[2], g = b[3], l = b[4], a = 0; 80 > a; a++) {
if (16 > a) m[a] = d[e + a] | 0;
else {
var c = m[a - 3] ^ m[a - 8] ^ m[a - 14] ^ m[a - 16];
m[a] = c << 1 | c >>> 31
}
c = (n << 5 | n >>> 27) + l + m[a];
c = 20 > a ? c + ((j & h | ~j & g) + 1518500249) : 40 > a ? c + ((j ^ h ^ g) + 1859775393) : 60 > a ? c + ((j & h | j & g | h & g) - 1894007588) : c + ((j ^ h ^ g) - 899497514);
l = g;
g = h;
h = j << 30 | j >>> 2;
j = n;
n = c
}
b[0] = b[0] + n | 0;
b[1] = b[1] + j | 0;
b[2] = b[2] + h | 0;
b[3] = b[3] + g | 0;
b[4] = b[4] + l | 0
},
_doFinalize: function () {
var d = this._data,
e = d.words,
b = 8 * this._nDataBytes,
g = 8 * d.sigBytes;
e[g >>> 5] |= 128 << 24 - g % 32;
e[(g + 64 >>> 9 << 4) + 14] = Math.floor(b / 4294967296);
e[(g + 64 >>> 9 << 4) + 15] = b;
d.sigBytes = 4 * e.length;
this._process();
return this._hash
},
clone: function () {
var e = d.clone.call(this);
e._hash = this._hash.clone();
return e
}
});
g.SHA1 = d._createHelper(l);
g.HmacSHA1 = d._createHmacHelper(l)
})();
(function () {
var g = CryptoJS,
l = g.enc.Utf8;
g.algo.HMAC = g.lib.Base.extend({
init: function (e, d) {
e = this._hasher = new e.init;
"string" == typeof d && (d = l.parse(d));
var g = e.blockSize,
k = 4 * g;
d.sigBytes > k && (d = e.finalize(d));
d.clamp();
for (var p = this._oKey = d.clone(), b = this._iKey = d.clone(), n = p.words, j = b.words, h = 0; h < g; h++) n[h] ^= 1549556828, j[h] ^= 909522486;
p.sigBytes = b.sigBytes = k;
this.reset()
},
reset: function () {
var e = this._hasher;
e.reset();
e.update(this._iKey)
},
update: function (e) {
this._hasher.update(e);
return this
},
finalize: function (e) {
var d = this._hasher;
e = d.finalize(e);
d.reset();
return d.finalize(this._oKey.clone().concat(e))
}
})
})();
//使用算法
// var key = "f7205fffe445421fdssdfsdfdsfs"
// var sha1_result = CryptoJS.HmacSHA1("helloword", key)
// console.log('-------',sha1_result.toString())
export default CryptoJS;