WeChat applet calls iFlytek spoken language evaluation code

To call the iFlytek spoken language evaluation code in the WeChat applet, you first need to register an iFlytek open platform account and create an application to obtain the AppId. Then follow these steps:

  1. Introduce iFlytek SDK resource files: Add iFlytek SDK references in the "subpackages" field or "usingComponents" field of the app.json file in the root directory of the mini program.
    For example:
"usingComponents": {
  "ifly-recognition": "path/to/ifly-recognition.wxss"
}
  1. In the js file of the mini program page, use the require function to introduce the iFlytek SDK and initialize the iFlytek SDK.
    For example:
const ifly = require('path/to/ifly.js')

// 初始化讯飞SDK
const appkey = 'your_appkey'
ifly.initSDK({ appId: 'your_appid', appkey: appkey })
  1. Use the iFlytek speaking evaluation function on the mini program page:
<ifly-recognition type="sentence" bindresult="onRecognitionResult"></ifly-recognition>
<button bindtap="startRecognition">开始评测</button>
<button bindtap="stopRecognition">停止评测</button>
  1. In the js file of the mini program page, implement the functions of starting and stopping the evaluation:
Page({
  data: {
    recognition: null
  },

  startRecognition() {
    const rec = ifly.createRecognition()
    rec.on('result', this.onRecognitionResult)
    rec.start()
    this.setData({ recognition: rec })
  },

  stopRecognition() {
    const { recognition } = this.data
    if (recognition) {
      recognition.stop()
      recognition.off('result', this.onRecognitionResult)
      this.setData({ recognition: null })
    }
  },

  onRecognitionResult(result) {
    console.log(result)
  }
})

The above code is only an example, and the specific implementation needs to be called according to the API provided by iFlytek SDK. Pay attention to replace "your_appid" and "your_appkey" in the sample code with the AppId and AppKey you obtained when registering on the iFlytek Open Platform.

Please note that the above code only provides the basic process of iFlytek speaking evaluation. For more detailed implementation, please refer to the official iFlytek documentation or the sample code provided by the SDK.

Guess you like

Origin blog.csdn.net/qq_32134891/article/details/131414032