Home>Article>Web Front-end> How to implement audio recognition function in uniapp

How to implement audio recognition function in uniapp

PHPz
PHPz Original
2023-07-05 14:18:09 2177browse

How to implement audio recognition function in uniapp

With the continuous development of artificial intelligence technology, speech recognition has been widely used in mobile applications and the Internet field. It is becoming easier and easier to implement the audio recognition function in uniapp. This article will introduce how to use Baidu Smart Cloud API in uniapp to realize the audio recognition function, and attach the corresponding code examples.

1. Preparation

  1. Register a Baidu Smart Cloud account and activate the speech recognition service
    Register an account on the Baidu Smart Cloud official website and activate speech recognition according to the official documents service and obtain the corresponding API Key and Secret Key.
  2. Create a uniapp project
    Create a uniapp project in HBuilder X, enter the project directory, open the manifest.json file, and add the following permissions:

    "permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" }, "scope.record": { "desc": "将要使用录音功能" } }

2. Introduce the Baidu speech recognition plug-in
In the project root directory, open the terminal and execute the following command:

npm i uni-baidu-voice-recognition

3. Use the Baidu speech recognition plug-in
In the page where the audio recognition function is required Introducing the plug-in:

 

When using speech recognition, we can start recording through thevoiceRecog.start()method and return the speech recognition results through the Promise object. Call thevoiceRecog.stop()method to stop recording.

4. Configure Baidu Speech Recognition Plug-in
In the project root directory, create a folder nameduni-baidu-voice-recognition, and create itmanifest.jsonfile is used to configure plug-in information. Add the following content to the file:

{ "minPlatformVersion": "1060", "name": "uni-baidu-voice-recognition", "version": "1.0.0", "description": "百度语音识别插件", "main": "index.js" }

Create theindex.jsfile under theuni-baidu-voice-recognitionfolder to implement speech recognition Function. Add the following code to the file:

import VoiceRecogPlugin from "voice-module" export default { start(options) { return new Promise((resolve, reject) => { VoiceRecogPlugin.start(options) .then(res => { resolve(res) }) .catch(err => { reject(err) }) }) }, stop() { VoiceRecogPlugin.stop() } }

5. Use the APP Key and Secret Key of the Baidu speech recognition plug-in
In the project root directory, open thehbuilder-config.jsonfile , add the following content:

"apps": [ { "type": "uni-app", "appid": "你的appid", "key": "你的key", "secret": "你的secret" } ]

Replace "your appid" in the above code with the APP Key you applied for on Baidu Smart Cloud, and replace "your key" and "your secret" with your API Key and Secret Key obtained from Baidu Smart Cloud.

At this point, the work of implementing the audio recognition function in uniapp has been completed. You can introduce a plug-in into the page where you need to use audio recognition, and use the corresponding methods in the button click event to start and stop recording. By calling the speech recognition API, you can obtain the results of speech recognition and process them accordingly.

Summary
This article introduces how to implement the audio recognition function in uniapp and provides corresponding code examples. By using Baidu Smart Cloud's API, we can easily add audio recognition functionality to uniapp, bringing more possibilities to our applications. I hope this article will help you implement audio recognition function in uniapp.

The above is the detailed content of How to implement audio recognition function in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn