小程序使用 微信同声传译

262 阅读1分钟

在编写小程序时有需求语音输入并转文字,以前h5端使用过科大讯飞的工具库,在写小程序时发现了一个插件:微信同声传译

使用方式 (uniapp):

  1. 进入微信小程序管理后台: mp.weixin.qq.com/
  2. 进入设置 -> 第三方设置 -> 插件管理

image.png

  1. 添加插件,搜索微信同声传译

tip: 如果找不到就去上面的 服务市场 搜索添加即可(好像个人申请不了)

  1. 记住这appId和版本号

image.png

  1. uniapp打开源码视图填入provider就是上面的appId,并不是小程序的appId

image.png

  1. 页面中引入和使用

image.png

  1. 代码
<template>
    ...
    <view class="t_cen" @touchstart="streamRecord" @touchend="endStreamRecord"></view>
    ...
</template>

<script>
    var plugin = requirePlugin("WechatSI")
    let manager = plugin.getRecordRecognitionManager()
    export default {
        data() {
            return {}
        },
        onLoad() {
            this.initRecord()
        },
        methods: {
            streamRecord() {
                console.log('开始')
                this.isVoice = true;
                manager.start({
                        lang: 'zh_CN',
                })
                },
            endStreamRecord() {
                this.isVoice = false;
                console.log('结束')
                manager.stop()
            },
           initRecord() {
                manager.onRecognize = (res) => {
                    this.currentText = res.result
                }
                manager.onStop = (res) => {
                    if (res.result == '') return
                    this.currentText = res.result
               }
           }
        }
    }
</script>