千家信息网

微信小程序语音识别、语音合成代码怎么写

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,今天小编给大家分享一下微信小程序语音识别、语音合成代码怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面
千家信息网最后更新 2025年02月07日微信小程序语音识别、语音合成代码怎么写

今天小编给大家分享一下微信小程序语音识别、语音合成代码怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

使用步骤:

1、 在微信公众平台配置,找到设置-第三方设置-插件管理-点击增加插件,

搜索微信同声传译并增加

2、 在项目根目录app.json文件中配置

"plugins": {

"WechatSI": {

"version": "0.3.4",

"provider": "wx069ba97219f66d99"

}

},

语音合成:

在pages的js中加入插件初始化代码

const innerAudioContext = wx.createInnerAudioContext();

innerAudioContext.autoplay = true;

const plugin = requirePlugin('WechatSI');

因为语音合成原理是微信同声传译是在同声传译后端生产录音,下载播放录音。可以在页面加载阶段生产录音,在使用的地方播放录音,就不会有推迟。

//在全局定义变量

var remoteAudio = null;

//在开始阶段加载

plugin.textToSpeech({

lang: "en_US",

tts: true,

content: word,

success: function(res) {

console.log("succ tts", res.filename)

// this.playAudio(res.filename);

remoteAudio = res.filename;

},

fail: function(res) {

console.log("fail tts", res)

}

})

},

//在实际需要使用语音合成地方

innerAudioContext.stop();

console.log("remoteAudio: " + remoteAudio);

innerAudioContext.src = remoteAudio;

innerAudioContext.play();

innerAudioContext.onError((e) => {

console.log(e.errMsg)

console.log(e.errCode)

})

语音识别:

在pages的js中加入插件初始化代码

//引入插件:微信同声传译

const plugin = requirePlugin('WechatSI');

//获取全局唯一的语音识别管理器recordRecoManager

const manager = plugin.getRecordRecognitionManager();

// 设置采集声音参数

const options = {

sampleRate: 44100,

numberOfChannels: 1,

encodeBitRate: 192000,

format: 'aac'

}

在 onload()中加入初始化代码

//识别语音

this.initRecord();

在需要加入语音识别地方加入下面代码:

//语音 --按住说话

touchStart: function(e) {

wx.vibrateShort() //按键震动效果(15ms)

manager.start(options)

this.setData({

recordState: true, //录音状态为真

tips: '松开结束',

})

},

//语音 --松开结束

touchEnd: function(e) {

// 语音结束识别

manager.stop();

this.setData({

recordState: false,

})

},

//识别语音 -- 初始化

initRecord: function() {

const that = this;

// 有新的识别内容返回,则会调用此事件

manager.onRecognize = function(res) {

console.log(res)

}

// 正常开始录音识别时会调用此事件

manager.onStart = function(res) {

console.log("成功开始录音识别", res)

}

// 识别错误事件

manager.onError = function(res) {

console.error("error msg:", res.retcode, res.msg)

}

//识别结束事件

manager.onStop = function(res) {

console.log('..............结束录音')

console.log('录音总时长 -->' + res.duration + 'ms');

console.log('语音内容 --> ' + res.result);

if (res.result == '') {

wx.showModal({

title: '提醒',

content: '听不清楚,请重新说一遍!',

showCancel: false,

success: function(res) {}

})

return;

}

//下面有些代码有少量业务代码,要根据自己实际进行替换

if(res.result == this.myword){

that.setData({

content: that.myword + '读音正确' //去掉自动增加的句号

})

next();

}else{

that.setData({

recordState: false, //录音状态为真

content: that.myword +'读音不准',

})

plugin.textToSpeech({

lang: "en_US",

tts: true,

content: that.myword,

success: function(res) {

console.log("succ tts", res.filename)

},

fail: function(res) {

console.log("fail tts", res)

}

})

}

}

},

以上就是"微信小程序语音识别、语音合成代码怎么写"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。

0