简介:本文详解Vue项目对接百度语音识别API的全流程,包含环境配置、代码实现、异常处理及优化建议,助力开发者快速构建智能语音交互功能。
随着智能交互需求的增长,语音识别技术已成为Web应用的重要功能模块。百度语音识别API凭借其高准确率、低延迟和稳定的服务能力,成为开发者首选方案之一。在Vue框架中集成该服务,可快速实现浏览器端的实时语音转文本功能,适用于智能客服、语音输入、无障碍访问等场景。
# 使用Vue CLI创建项目vue create vue-baidu-asrcd vue-baidu-asrnpm install axios ws --save # 安装HTTP与WebSocket库
建议采用环境变量存储敏感信息,创建.env.local文件:
VUE_APP_BAIDU_API_KEY=your_api_keyVUE_APP_BAIDU_SECRET_KEY=your_secret_key
在vue.config.js中配置:
module.exports = {pluginOptions: {dotenv: {path: '.env.local'}}}
// src/utils/baiduASR.jsimport WebSocket from 'ws';class BaiduASR {constructor() {this.token = '';this.ws = null;this.result = '';}async getAccessToken() {const res = await axios.get('https://aip.baidubce.com/oauth/2.0/token', {params: {grant_type: 'client_credentials',client_id: process.env.VUE_APP_BAIDU_API_KEY,client_secret: process.env.VUE_APP_BAIDU_SECRET_KEY}});return res.data.access_token;}async startRecognition() {this.token = await this.getAccessToken();const wsUrl = `wss://vop.baidu.com/websocket_asr?token=${this.token}&cuid=vue-asr&dev_pid=1537`;this.ws = new WebSocket(wsUrl);this.ws.onopen = () => {console.log('WebSocket连接建立');// 发送开始指令this.ws.send(JSON.stringify({'user_id': 'vue-demo','format': 'audio/pcm;rate=16000','channel': 1,'token': this.token}));};this.ws.onmessage = (event) => {const data = JSON.parse(event.data);if (data.result) {this.result += data.result.replace(/[;,.]/g, '');}};}sendAudio(audioChunk) {if (this.ws.readyState === WebSocket.OPEN) {this.ws.send(audioChunk);}}}
<!-- src/components/AudioRecorder.vue --><template><div><button @click="startRecording" :disabled="isRecording">开始录音</button><button @click="stopRecording" :disabled="!isRecording">停止录音</button><div>识别结果:{{ recognitionResult }}</div></div></template><script>import BaiduASR from '@/utils/baiduASR';export default {data() {return {isRecording: false,recognitionResult: '',mediaRecorder: null,audioChunks: [],asrClient: new BaiduASR()};},methods: {async startRecording() {try {const stream = await navigator.mediaDevices.getUserMedia({ audio: true });this.mediaRecorder = new MediaRecorder(stream, {mimeType: 'audio/pcm',audioBitsPerSecond: 256000});this.audioChunks = [];this.isRecording = true;this.mediaRecorder.ondataavailable = (event) => {this.audioChunks.push(event.data);// 实时发送音频数据if (this.audioChunks.length > 0) {const blob = new Blob(this.audioChunks, { type: 'audio/pcm' });const reader = new FileReader();reader.onload = () => {this.asrClient.sendAudio(reader.result);};reader.readAsArrayBuffer(blob);}};this.mediaRecorder.start(100); // 每100ms发送一次数据await this.asrClient.startRecognition();} catch (error) {console.error('录音失败:', error);}},stopRecording() {this.mediaRecorder.stop();this.mediaRecorder.stream.getTracks().forEach(track => track.stop());this.isRecording = false;// 可以在这里处理最终的识别结果}},beforeDestroy() {if (this.mediaRecorder) {this.mediaRecorder.stream.getTracks().forEach(track => track.stop());}}};</script>
对于一次性文件识别,可采用以下方式:
// src/utils/baiduASRRest.jsexport async function recognizeAudio(audioFile) {const token = await getAccessToken(); // 实现同上const formData = new FormData();formData.append('audio', audioFile);formData.append('format', 'wav');formData.append('rate', 16000);formData.append('channel', 1);formData.append('token', token);formData.append('cuid', 'vue-rest-demo');formData.append('dev_pid', 1537);const response = await axios.post('https://vop.baidu.com/server_api',formData,{headers: {'Content-Type': 'multipart/form-data'}});return response.data.result;}
function applyNoiseSuppression(audioContext, inputBuffer) {const scriptNode = audioContext.createScriptProcessor(4096, 1, 1);scriptNode.onaudioprocess = (audioProcessingEvent) => {const inputData = audioProcessingEvent.inputBuffer.getChannelData(0);// 实现简单的降噪算法const outputData = inputData.map(sample => {return Math.abs(sample) < 0.1 ? 0 : sample;});const outputBuffer = audioProcessingEvent.outputBuffer;const output = outputBuffer.getChannelData(0);output.set(outputData);};return scriptNode;}
// 在WebSocket类中添加this.ws.onerror = (error) => {console.error('WebSocket错误:', error);this.$emit('error', {code: 'WS_ERROR',message: 'WebSocket连接异常'});};this.ws.onclose = (event) => {if (event.code !== 1000) {console.warn('WebSocket非正常关闭:', event.code);// 实现重连逻辑}};
function postProcessResult(rawText) {// 去除标点符号const noPunctuation = rawText.replace(/[.,\/#!$%\^&*;:{}=\-_`~()]/g, '');// 中文繁简转换(可选)// 添加标点符号(可选)return noPunctuation;}
修改dev_pid参数即可支持不同语言:
结合百度UNIT平台实现意图识别:
async function getSemanticResult(text) {const token = await getAccessToken();const response = await axios.post('https://aip.baidubce.com/rpc/2.0/unit/service/v1/chat',{log_id: Date.now(),version: '2.0',service_id: 'your_service_id',session_id: '',request: {query: text,user_id: 'vue-demo'}},{params: {access_token: token}});return response.data;}
在vue.config.js中配置代理:
module.exports = {devServer: {proxy: {'/baidu-asr': {target: 'https://vop.baidu.com',changeOrigin: true,pathRewrite: {'^/baidu-asr': ''}}}}}
本文通过完整的代码示例和详细的实现步骤,展示了在Vue项目中集成百度语音识别API的全过程。开发者可根据实际需求选择WebSocket流式识别或REST API文件识别方案,并通过优化音频处理、错误处理和结果后处理等环节,构建出稳定高效的语音识别系统。建议在实际部署前进行充分的测试,特别是不同浏览器和设备下的兼容性测试,以确保最佳用户体验。