前端 vue 或者麦克风,智能语音识别和播放功能
1. 终端安装
npm install recordrtc
2.引入
import RecordRTC from 'recordrtc'
3.html(根据自己业务更改)
<div class="Page"><el-form ref="mainFormRef" class="mainForm" :model="main_form" label-width="100px" label-position="top"><el-form-item :label="'热词设置:\n(一行一个关键字,空格隔开权重,如将1号屏和3号屏调换 20)'"><el-input type="textarea" v-model="main_form.hotWords" placeholder="请输入热词":autosize="{ minRows: 5, maxRows: 15 }"></el-input></el-form-item><el-form-item label="语音识别结果显示:"><div :class="resultDetails && resultDetails.status <= 0 ? 'result_content r_h_input_red' : 'result_content'">{{main_form.result }}</div></el-form-item><el-form-item label="声纹采集:" prop="file"><div class="voiceGather_btn"><el-select :disabled="voiceStatus" style="width: 100%" v-model="main_form.chooseMicDeviceId"placeholder="请选择麦克风"><el-option v-for="item in Mic" :key="item.deviceId" :label="item.label" :value="item.deviceId"></el-option></el-select><div class="voiceGather" v-if="main_form.chooseMicDeviceId != ''"><el-button style="margin-left: 20px" @click="voiceInput" :loading="startLoading">{{ voiceStatus ? "取消录音" :"开始录音" }}</el-button></div></div><div class="voiceGather_btn"><audio controls v-if="recordedBlob" :src="recordedBlob"></audio></div></el-form-item></el-form></div>
4.data初始化数据
data() {return {recorder: '',voiceStatus: false, main_form: {chooseMicDeviceId: '', hotWords: '', result: '', },Mic: [], RMSList: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0,],audioURL: null,file: null,resultDetails: {},startLoading: false,recordedBlob: null, };},
5.mounted获取getMic
mounted() {this.getMic()
}
6.methods中开始录音和结束之后上传到后台服务器
methods() {getMic() {let that = this;if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {navigator.mediaDevices.enumerateDevices().then(function (devices) {devices.forEach(function (device) {if (device.kind === 'audioinput') { if (device.deviceId != 'default' && device.deviceId != 'communications') {that.Mic.push(device)}}that.main_form.chooseMicDeviceId = that.Mic[0] ? that.Mic[0].deviceId : ''});})stream.getTracks().forEach(track => track.stop());})}},voiceInput() {if (this.voiceStatus) {this.stopRecord() } else { this.resultDetails ={}this.main_form.result = ''this.recordedBlob = nullthis.startRecord()}},startRecord() {console.log('startRecord:鼠标摁下------------------------------')var that = thisthis.voiceStatus = truewindow.navigator.mediaDevices.getUserMedia({ audio: { deviceId: this.main_form.chooseMicDeviceId } }).then((stream) => {this.stream = stream;this.getVoice()this.recorder = RecordRTC(stream, {type: 'audio',mimeType: 'audio/wav',recorderType: RecordRTC.StereoAudioRecorder,desiredSampRate: 16000,numberOfAudioChannels: 1, timeSlice: 1000,ondataavailable: this.sendData,});this.recorder.startRecording();}).catch(function (err) {alert('当前浏览器不支持开启麦克风!');that.voiceStatus = false});},sendData(blob) {return},stopRecord() {console.log('stopRecord:鼠标放开------------------------------')if (this.recorder != null) {this.startLoading = truelet recorder = this.recorderrecorder.stopRecording(() => {const blob = this.recorder.getBlob(); this.recordedBlob = URL.createObjectURL(blob);var BB = new Blob([blob], { 'type': 'audio/wav; codecs=opus' })let file = new window.File([BB], '测试.wav')this.file = fileconsole.log('获取到文件流上传到后台---------------------', this.file);this.uploadSubmit();});let stream = this.stream;clearInterval(this.timer1);stream.getAudioTracks().forEach(track => track.stop());}},uploadSubmit() {uploadAudio(this.file, this.main_form.hotWords).then(res => {this.resultDetails = res.data.data || {}this.main_form.result = res.data.data.result || ''this.voiceStatus = falsethis.startLoading = false}).catch(err => {this.voiceStatus = falsethis.startLoading = false})},getVoice() {const audioContext = new (window.AudioContext || window.webkitAudioContext)()const mediaStreamSource = audioContext.createMediaStreamSource(this.stream)const analyserNode = audioContext.createAnalyser()mediaStreamSource.connect(analyserNode)const dataArray = new Uint8Array(analyserNode.frequencyBinCount);function getVolume() {analyserNode.getByteFrequencyData(dataArray);let sum = 0;for (let i = 0; i < dataArray.length; i++) {sum += dataArray[i];}const averageVolume = sum / dataArray.length;return averageVolume;}this.timer1 = setInterval(() => {const volume = getVolume();console.log('音量:', Math.round(volume));}, 1000);},
}