WeChat Mini Program uses simultaneous interpretation to implement speech recognition

WeChat Mini Program uses simultaneous interpretation to implement speech recognition

I use the simultaneous interpretation voice recognition function to realize the voice search function on the homepage of the WeChat applet. If you are also like that, congratulations. You can press ctrl+c, ctrl+v to change it again. If you are not like that, don’t leave in a hurry. Reading my article will be helpful to you!

First, on the WeChat public platform (which is the background of the mini program), go to Settings in the left menu bar --> Plugin Management under Third-party Settings --> Add --> Search for Simultaneous Interpretation --> Click Add

The next step is to add some settings in the code.

If you are using WeChat developer tools to develop mini-programs, you need to add the following code to the app.json file.

// app.json
{
    ...
    "plugins": {
        ...
        "WechatSI": {
            "version": "0.3.4", // This is the version of simultaneous interpretation (you can also check the latest version of simultaneous interpretation added in WeChat public platform)
            "provider": "wx069ba97219f66d99" // This is the ID of the simultaneous interpreter
        }
    }
}

If you are using Hbuildex to develop a small program, you need to add modifications in the source code view of the manifest.json file.

Find mp-weixin in the source code view, and then add and modify it according to the following code

// manifest.json
/* Mini-program related*/
"mp-weixin": {
    "appid": "xxxxxxxxxx", // This is the AppId of your applet
    ...
    "plugins": {
        "WechatSI": {
            "version": "0.3.4", // This is the version of simultaneous interpretation (you can also check the latest version of simultaneous interpretation added in WeChat public platform)
            "provider": "wx069ba97219f66d99" // This is the ID of the simultaneous interpreter
        }
    }
}

After completing the above steps, you can develop according to the official documentation

The following is my function implementation code

// index.vue Here I only write the voice button in my page layout (simplified)
<template>
    <div @click="yuyin" class="yuyin-icon">
        <img :src="baseUrlImg+'/yuyin.png'" alt="" class="img" />
    </div>
</template>
<script>
    export default {
        data() {
            return {
                // This is the content of the search box search_word: ''
            }
        },
        methods: {
            // Voice click eventyuyin: function() {
                var that = this
                // Initiate an authorization request to the useruni.authorize({
                    scope: 'scope.record', // Get the recording function, that is, the microphone permission success: (res) => {
                        // User authorized to use microphone permission to call voice search event function that.plugin()
                    },
                    // The user does not have permission to use the microphone. Execute the following code fail(res) {
                        // Display a modal pop-up window to remind the user that the microphone permission is not enabled uni.showModal({
                            content: 'It is detected that you have not enabled the microphone permission, please keep the microphone permission enabled',
                            confirmText: 'Go to open',
                            showCancel: false,
                            success: (res) => {
                                console.log(res)
                                if(res.confirm) {
                                    // Open the client applet settings interface and return the result of the user's settings uni.openSetting({
                                        success: (res) => {
                                            console.log(res)
                                            if(res.authSetting['scope.record'] == false) {
                                                that.plugin()
                                            }
                                        }
                                    })
                                } else {
                                    uni.navigateBack({
                                        delta: 1
                                    })
                                }
                            }
                        })
                    }
                })
            }
            // Voice search plugin () {
                var that = this
                var plugin = requirePlugin('WechatSI')
                var manager = plugin.getRecordRecognitionManager()
                // Set the recording parameters manager.start({
                    duration: 5000, // time lang: "zh_CN" // language })
                // Start recording manager.onStart = function(res) {
                    console.log("Recording recognition started successfully", res)
                    if(res.msg == 'Ok') {
                        // Prompt the user that recording is in progressuni.showToast({
                            title: 'Recognizing voice...',
                            duration: 5000,
                            icon: 'loading'
                        })
                    }
                }
                // Recording ends manager.onStop = function(res) {
                    // Prompt the user that they are jumping to the search page (because when I did it, it would take 1 to 2 seconds to jump, so I set a prompt box)
                    uni.showToast({
                        title: 'Redirecting...',
                        duration: 1500,
                        icon: 'success'
                    })
                    // Translate the recognized speech into text plugin.translate({
                        lfrom: 'en_US',
                        lto: 'zh_CN',
                        content: res.result,
                        success: function(res) {
                            if(res.retcode == 0) {
                                // (This is the case for iPhone, not sure about Android) Speech recognition sometimes adds a symbol at the end if (res.result.charAt(res.result.length - 1) == '.' || res.result.charAt(res.result.length - 1) == '.') {
                                    res.result = res.result.substr(0, res.result.length - 1);
                                }
                                // Put the translated content into the search box that.search_word = res.result
                                // Code that performs the search function that.searchName()
                            } else {
                                console.log('Translation failed', res)
                            }
                        },
                        fail: function(res) {
                            console.log('Network failed', res)
                            // When the user speaks softly or does not speak, these two errors will be reported if(res.retcode == -10001 || res.retcode == -10002) {
                                uni.showToast({
                                    title: 'I didn't hear what you said',
                                    duration: 1000,
                                    icon: 'error'
                                })
                            }
                        }
                    })
                }
                 // Print error information manager.onError = function(res) {
                    console.error('error msg', res.msg)
                }
            }
        }
    }
</script>

This is the end of this article about how WeChat Mini Program uses simultaneous interpretation to implement voice recognition. For more relevant Mini Program voice recognition content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet implementation code for real-time speech recognition via websocket
  • WeChat applet implements voice recognition to text function and the pitfalls encountered
  • Detailed explanation of WeChat applet and Baidu's speech recognition interface
  • Quickly implement the mini program voice recognition function in 30 minutes

<<:  Mysql queries the transactions being executed and how to wait for locks

>>:  How to install mongodb 4.2 using yum on centos8

Recommend

MySQL dual-master (master-master) architecture configuration solution

In enterprises, database high availability has al...

A pitfall and solution of using fileReader

Table of contents A pitfall about fileReader File...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

Vuex implements a simple shopping cart

This article example shares the specific code of ...

MySQL FAQ series: How to avoid a sudden increase in the size of the ibdata1 file

0. Introduction What is the ibdata1 file? ibdata1...

MySQL 8.0.22.0 download, installation and configuration method graphic tutorial

MySQL 8.0.22 download, installation and configura...

Mysql 5.7.18 Using MySQL proxies_priv to implement similar user group management

Use MySQL proxies_priv (simulated role) to implem...

Detailed explanation of monitoring Jenkins process based on zabbix

1. Monitoring architecture diagram 2. Implementat...

Using Openlayer in Vue to realize loading animation effect

Note: You cannot use scoped animations! ! ! ! via...

Detailed explanation of mysql user variables and set statement examples

Table of contents 1 Introduction to user variable...

Vue implements image dragging and sorting

This article example shares the specific code of ...

How to redirect URL using nginx rewrite

I often need to change nginx configuration at wor...

Detailed explanation of virtual DOM and diff algorithm in react

The role of virtual DOM First of all, we need to ...

Tutorial on downloading, installing, configuring and using MySQL under Windows

Overview of MySQL MySQL is a relational database ...

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...