简介:本文详细介绍了在uniapp框架中接入百度语音识别API,实现语音转文字功能,并通过指令匹配机制触发相应操作,适用于智能家居、语音助手等场景。
在移动应用开发中,语音交互已成为提升用户体验的重要方向。uniapp作为跨平台开发框架,支持多端部署,结合百度语音识别API,可快速实现语音转文字功能,并通过指令匹配机制触发相应操作。本文将详细介绍如何在uniapp中完成这一流程,涵盖API接入、语音采集、文字识别、指令匹配及后续操作的全链路实现。
首先需在百度智能云平台注册账号,进入“语音技术”服务模块,创建应用并获取API Key及Secret Key。这两个密钥是调用API的身份凭证,需妥善保管。
uniapp通过插件市场或原生插件方式接入百度语音识别。推荐使用官方提供的JavaScript SDK,或通过uni-app原生插件市场下载封装好的组件。安装后需在manifest.json中配置权限:
{"permission": {"scope.record": {"desc": "需要录音权限以采集语音"}}}
在页面加载时初始化客户端,传入API Key和Secret Key:
import BaiduASR from '@/components/baidu-asr.js';export default {data() {return {asrClient: null};},onLoad() {this.asrClient = new BaiduASR({apiKey: 'YOUR_API_KEY',secretKey: 'YOUR_SECRET_KEY'});}};
通过uni.authorize申请录音权限,并在用户拒绝时提供友好提示:
async requestRecordPermission() {try {await uni.authorize({scope: 'scope.record'});return true;} catch (e) {uni.showModal({title: '提示',content: '需要录音权限以使用语音功能',showCancel: false});return false;}}
调用百度语音识别API的实时流式识别接口,需处理音频流传输及结果回调:
async startRecognition() {if (!await this.requestRecordPermission()) return;const stream = await uni.getRecorderManager().start({format: 'pcm',sampleRate: 16000,encodeBitRate: 16000});this.asrClient.start({audioSource: stream,onResult: (result) => {const text = result.result;this.handleRecognizedText(text);},onError: (err) => {console.error('识别错误:', err);}});}
在用户结束语音或超时时,停止识别并释放资源:
stopRecognition() {this.asrClient.stop();uni.getRecorderManager().stop();}
定义指令与操作的映射关系,可采用键值对或正则表达式匹配:
const COMMAND_MAP = {'打开灯光': { action: 'turnOnLight', params: { device: 'livingRoom' } },'关闭空调': { action: 'turnOffAC', params: {} },'播放音乐': { action: 'playMusic', params: { category: 'default' } }};// 或使用正则表达式匹配模糊指令const REGEX_COMMANDS = [{ pattern: /^播放(.*)音乐$/, action: 'playMusic', extract: (match) => ({ category: match[1] }) }];
对识别结果进行预处理(如去除标点、统一大小写),然后匹配指令:
handleRecognizedText(text) {const processedText = text.trim().toLowerCase();// 精确匹配const exactMatch = COMMAND_MAP[processedText];if (exactMatch) {this.executeAction(exactMatch.action, exactMatch.params);return;}// 正则匹配for (const { pattern, action, extract } of REGEX_COMMANDS) {const match = processedText.match(pattern);if (match) {this.executeAction(action, extract(match));return;}}uni.showToast({title: '未识别指令',icon: 'none'});}
根据匹配结果调用对应方法,并给予用户反馈:
executeAction(action, params) {switch (action) {case 'turnOnLight':this.controlDevice('light', 'on', params.device);break;case 'playMusic':this.playMusic(params.category);break;// 其他操作...}},controlDevice(type, command, device) {uni.request({url: `https://your-api.com/devices/${device}`,method: 'POST',data: { type, command },success: () => {uni.showToast({ title: `${device} ${type} 已${command}` });}});}
以下是一个简化版的完整实现:
<template><view><button @click="startRecognition">开始语音</button><button @click="stopRecognition">停止语音</button><text>识别结果: {{ recognizedText }}</text></view></template><script>import BaiduASR from '@/components/baidu-asr.js';export default {data() {return {asrClient: null,recognizedText: '',COMMAND_MAP: {'打开灯光': { action: 'controlDevice', params: { type: 'light', command: 'on' } }}};},onLoad() {this.asrClient = new BaiduASR({apiKey: 'YOUR_API_KEY',secretKey: 'YOUR_SECRET_KEY'});},methods: {async startRecognition() {if (!await this.requestRecordPermission()) return;const stream = await uni.getRecorderManager().start({format: 'pcm',sampleRate: 16000});this.asrClient.start({audioSource: stream,onResult: (result) => {this.recognizedText = result.result;this.handleRecognizedText(result.result);}});},handleRecognizedText(text) {const command = this.COMMAND_MAP[text.trim().toLowerCase()];if (command) {this[command.action](command.params.type, command.params.command);}},controlDevice(type, command) {uni.request({url: `https://your-api.com/devices`,method: 'POST',data: { type, command },success: () => {uni.showToast({ title: `${type} 已${command}` });}});},async requestRecordPermission() {try {await uni.authorize({ scope: 'scope.record' });return true;} catch (e) {uni.showModal({ title: '需要录音权限', showCancel: false });return false;}},stopRecognition() {this.asrClient.stop();uni.getRecorderManager().stop();}}};</script>
通过uniapp接入百度语音识别API,开发者可快速构建支持语音交互的跨平台应用。关键步骤包括API密钥配置、语音采集权限管理、实时识别流处理、指令匹配逻辑设计及操作执行反馈。实际应用中需结合业务场景优化指令库、提升识别准确率,并严格遵守隐私保护规范。此方案适用于智能家居控制、语音助手、车载系统等需要自然语言交互的场景,能有效提升用户操作便捷性。