Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write WeChat Mini Programs's speech recognition and speech synthesis code

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge about how to write WeChat Mini Programs's speech recognition and speech synthesis code. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Steps to use:

1. Configure on Wechat public platform, find Settings-third-party Settings-plug-in Management-Click to add plug-ins

Search for Wechat simultaneous interpretation and add

2. Configure in the project root app.json file

"plugins": {

"WechatSI": {

"version": "0.3.4"

"provider": "wx069ba97219f66d99"

}

}

Speech synthesis:

Add the plug-in initialization code to the js of pages

Const innerAudioContext = wx.createInnerAudioContext ()

InnerAudioContext.autoplay = true

Const plugin = requirePlugin ('WechatSI')

Because the principle of speech synthesis is that Wechat simultaneous interpretation produces audio recordings and downloads and plays them at the back end of simultaneous interpretation. You can produce the recording during the page load phase and play the recording where it is used without delay.

/ / define variables globally

Var remoteAudio = null

/ / load in the initial phase

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)

}

})

}

/ / where speech synthesis is actually needed

InnerAudioContext.stop ()

Console.log ("remoteAudio:" + remoteAudio)

InnerAudioContext.src = remoteAudio

InnerAudioContext.play ()

InnerAudioContext.onError ((e) = > {

Console.log (e.errMsg)

Console.log (e.errCode)

})

Speech recognition:

Add the plug-in initialization code to the js of pages

/ / introduction of plug-ins: simultaneous interpretation of Wechat

Const plugin = requirePlugin ('WechatSI')

/ / get the globally unique speech recognition Manager recordRecoManager

Const manager = plugin.getRecordRecognitionManager ()

/ / set sound acquisition parameters

Const options = {

SampleRate: 44100

NumberOfChannels: 1

EncodeBitRate: 192000

Format: 'aac'

}

Add initialization code to onload ()

/ / speech recognition

This.initRecord ()

Add the following code where you need to add speech recognition:

/ / Voice-- hold down and speak

TouchStart: function (e) {

Wx.vibrateShort () / / Button Vibration effect (15ms)

Manager.start (options)

This.setData ({

RecordState: true, / / recording status is true

Tips: 'loosen the end'

})

}

/ / Voice-- release and end

TouchEnd: function (e) {

/ / speech end recognition

Manager.stop ()

This.setData ({

RecordState: false

})

}

/ / speech recognition-initialization

InitRecord: function () {

Const that = this

/ / if new identification content is returned, this event will be called.

Manager.onRecognize = function (res) {

Console.log (res)

}

/ / this event will be called when recording recognition starts normally.

Manager.onStart = function (res) {

Console.log ("start recording recognition successfully", res)

}

/ / identify error events

Manager.onError = function (res) {

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

}

/ / identify the end event

Manager.onStop = function (res) {

Console.log ('. End recording')

Console.log ('Total recording time->' + res.duration + 'ms')

Console.log ('voice content-- >' + res.result)

If (res.result = ='') {

Wx.showModal ({

Title: 'reminder'

Content: "I can't hear you clearly. Please say it again!"

ShowCancel: false

Success: function (res) {}

})

Return

}

/ / some of the following codes have a small amount of business code, which should be replaced according to their own reality.

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

That.setData ({

Content: that.myword + 'correct pronunciation' / / remove the automatically added full stop

})

Next ()

} else {

That.setData ({

RecordState: false, / / recording status is true

Content: that.myword + 'incorrect pronunciation'

})

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)

}

})

}

}

}

These are all the contents of the article "how to write WeChat Mini Programs's speech recognition and speech synthesis code". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report