微信小程序语音识别

需求是:语音转文字

解决方案:微信同声传译插件

首先,需要在微信小程序后台添加插件:设置->第三方设置->插件管理->添加插件。(ps:搜索的名称是“微信同声传译”,不是 wechatSI)

然后,根据文档,在 app.json 中添加如下配置:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
……
"plugins": {
"WechatSI": {
"version": "0.3.4",
"provider": "wx069ba97219f66d99"
}
},
……
}
{ …… "plugins": { "WechatSI": { "version": "0.3.4", "provider": "wx069ba97219f66d99" } }, …… }
{
……
  "plugins": {
    "WechatSI": {
      "version": "0.3.4",
      "provider": "wx069ba97219f66d99"
    }
  },
……
}

再然后,在需要使用语音识别的页面 js 文件中添加如下代码(简单示例):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
Page({
/**
* 页面的初始数据
*/
data: {
value: '',
recording: false, // 正在录音
recordStatus: 2, // 状态: 0 - 录音中 1- 翻译中 2 - 翻译完成/二次翻译 暂无翻译需求
hasRecordAuth: false, // 是否已经语音授权
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function () {
// 获取录音授权
this.getRecordAuth()
// 识别结束
manager.onStop = this.recognizeStop
// 错误
manager.onError = this.handleRecordError
},
// 开始录音
streamRecord(e) {
if (!this.data.hasRecordAuth) {
wx.showToast({
title: '没有录音权限',
icon: 'none'
})
return
}
manager.start()
this.setData({
recordStatus: 0,
recording: true
})
},
// 结束录音
endRecord() {
console.log('record end1')
// 防止重复触发
if (!this.data.recording || this.data.recordStatus !== 0) {
return
}
this.setData({
recording: false,
recordStatus: 1
})
console.log('record end2')
manager.stop()
},
/**
* 语音识别结束
*/
recognizeStop(e) {
console.log(e)
let text = e.result || ''
text = text.replace(/。/g, '') // 因为识别后的文字会以句号结尾,这里干脆去掉所有的句号
if (text) {
this.setData({
recordStatus: 2,
})
} else {
wx.showToast({
title: '没有识别到语音',
icon: "none"
})
}
},
/**
* 识别报错
*/
handleRecordError(e) {
console.log(e)
wx.showToast({
title: '语音识别似乎有点小问题',
icon: 'none'
})
},
/**
* 录音授权
*/
getRecordAuth() {
let _self = this
wx.getSetting({
success(res) {
if (!res.authSetting['scope.record']) {
wx.authorize({
scope: 'scope.record',
success() {
_self.setData({
hasRecordAuth: true
})
console.log('record auto success')
}
})
} else {
_self.setData({
hasRecordAuth: true
})
}
}
})
},
})
var plugin = requirePlugin("WechatSI") let manager = plugin.getRecordRecognitionManager() Page({ /** * 页面的初始数据 */ data: { value: '', recording: false, // 正在录音 recordStatus: 2, // 状态: 0 - 录音中 1- 翻译中 2 - 翻译完成/二次翻译 暂无翻译需求 hasRecordAuth: false, // 是否已经语音授权 }, /** * 生命周期函数--监听页面加载 */ onLoad: function () { // 获取录音授权 this.getRecordAuth() // 识别结束 manager.onStop = this.recognizeStop // 错误 manager.onError = this.handleRecordError }, // 开始录音 streamRecord(e) { if (!this.data.hasRecordAuth) { wx.showToast({ title: '没有录音权限', icon: 'none' }) return } manager.start() this.setData({ recordStatus: 0, recording: true }) }, // 结束录音 endRecord() { console.log('record end1') // 防止重复触发 if (!this.data.recording || this.data.recordStatus !== 0) { return } this.setData({ recording: false, recordStatus: 1 }) console.log('record end2') manager.stop() }, /** * 语音识别结束 */ recognizeStop(e) { console.log(e) let text = e.result || '' text = text.replace(/。/g, '') // 因为识别后的文字会以句号结尾,这里干脆去掉所有的句号 if (text) { this.setData({ recordStatus: 2, }) } else { wx.showToast({ title: '没有识别到语音', icon: "none" }) } }, /** * 识别报错 */ handleRecordError(e) { console.log(e) wx.showToast({ title: '语音识别似乎有点小问题', icon: 'none' }) }, /** * 录音授权 */ getRecordAuth() { let _self = this wx.getSetting({ success(res) { if (!res.authSetting['scope.record']) { wx.authorize({ scope: 'scope.record', success() { _self.setData({ hasRecordAuth: true }) console.log('record auto success') } }) } else { _self.setData({ hasRecordAuth: true }) } } }) }, })
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    recording: false, // 正在录音
    recordStatus: 2, // 状态: 0 - 录音中 1- 翻译中 2 - 翻译完成/二次翻译 暂无翻译需求
    hasRecordAuth: false, // 是否已经语音授权
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    // 获取录音授权
    this.getRecordAuth()
    // 识别结束
    manager.onStop = this.recognizeStop
    // 错误
    manager.onError = this.handleRecordError
  },

  // 开始录音
  streamRecord(e) {
    if (!this.data.hasRecordAuth) {
      wx.showToast({
        title: '没有录音权限',
        icon: 'none'
      })
      return
    }
    manager.start()
    this.setData({
      recordStatus: 0,
      recording: true
    })
  },

  // 结束录音
  endRecord() {
    console.log('record end1')
    // 防止重复触发
    if (!this.data.recording || this.data.recordStatus !== 0) {
      return
    }
    this.setData({
      recording: false,
      recordStatus: 1
    })
    console.log('record end2')
    manager.stop()
  },

  /**
   * 语音识别结束
   */
  recognizeStop(e) {
    console.log(e)
    let text = e.result || ''
    text = text.replace(/。/g, '') // 因为识别后的文字会以句号结尾,这里干脆去掉所有的句号
    if (text) {
      this.setData({
        recordStatus: 2,
      })
    } else {
      wx.showToast({
        title: '没有识别到语音',
        icon: "none"
      })
    }
  },

  /**
   * 识别报错
   */
  handleRecordError(e) {
    console.log(e)
    wx.showToast({
      title: '语音识别似乎有点小问题',
      icon: 'none'
    })
  },

  /**
   * 录音授权
   */
  getRecordAuth() {
    let _self = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
            success() {
              _self.setData({
                hasRecordAuth: true
              })
              console.log('record auto success')
            }
          })
        } else {
          _self.setData({
            hasRecordAuth: true
          })
        }
      }
    })
  },
})