微信小程序之点击文字文字自动转语音进行播放,微信小程序文字识别转语音播放

微信小程序之点击文字文字自动转语音进行播放,微信小程序文字识别转语音播放

需求

一堆题目,题干需要在点击的时候进行语音朗读,不做音频上传,不然不便于维护

解决方案

点击查看微信官方文档:微信同声传译

使用流程

后台配置 mp.weixin.qq.com

设置 > 第三方设置 > 插件管理

小程序插件使用流程见文档:https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/using.html

微信同声传译插件使用:https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wx069ba97219f66d99&token=397787617&lang=zh_CN

小程序使用

//app.json

{

...

"plugins": {

...

"WechatSI": {

"version": "0.3.5",

"provider": "wx069ba97219f66d99"

}

}

}

//index.js

const plugin = requirePlugin("WechatSI")

plugin.textToSpeech({

lang: "zh_CN",

tts: true,

content: "一个常见的需求",

success: function(res) {

console.log("succ tts", res.filename) // 合成的语音文件存放地址

// 转换之后进行语音播放

let innerAudioContext = wx.createInnerAudioContext()

innerAudioContext.src = res.filename

innerAudioContext.play()

innerAudioContext.onEnded(()=>{

innerAudioContext = null // 播放完成释放内存

})

},

fail: function(err) {

console.log("fail tts", err)

}

})

相关推荐