1、使用插件,小程序后台设置-第三方服务-插件管理中搜索微信同声传译或腾讯云智能语音,使用这个插件即可实现实时语音识别。
微信同声传译不仅能实时识别,还是实时翻译成指定国家的语音,支持文本转语音合成,关键还是免费的
const plugin = requirePlugin('WechatSI');
const manager = plugin.getRecordRecognitionManager()
manager.onStop = function(res) {
console.log("record file path", res.tempFilePath)
console.log("result", res.result)
}
manager.onStart = function(res) {
console.log("成功开始录音识别", res)
}
manager.onError = function(res) {
console.error("error msg", res.msg)
}
plugin.textToSpeech({
lang: "zh_CN",
tts: true,
content: "一个常见的需求",
success: function(res) {
//这个res.filename是生成后的声音文件地址url,
console.log("succ tts", res.filename)
},
fail: function(res) {
console.log("fail tts", res)
}
})
manager.start({
duration: 3000,
lang: "zh_CN"
})
manager.stop()
plugin.translate({
lfrom:"en_US",
lto:"zh_CN",
content:"hello, this is the first time to test?",
success: function(res) {
if(res.retcode == 0) {
console.log("result", res.result)
} else {
console.warn("翻译失败", res)
}
},
fail: function(res) {
console.log("网络失败",res)
}
})文档:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/extended/translator.html腾讯云智能语音不仅能使用语音,还能合成语音,但是需要开通腾讯云服务,收费的:文档:https://cloud.tencent.com/document/product/1093/48982

2、使用第三方,例如百度、阿里云的实时语音识别api,通常是websocket的形式
阿里的paraformer实时识别引擎,能实时识别中文和其他方言,也非常便宜,开发文档:
https://help.aliyun.com/zh/model-studio/developer-reference/paraformer-real-time-speech-recognition-api
python示例代码
https://github.com/aliyun/alibabacloud-bailian-speech-demo/blob/master/samples/speech-recognition/recognize_speech_from_microphone/python/run.py
网友回复


