Vue如何使用js-audio-recorder插件實(shí)現(xiàn)錄音功能并將文件轉(zhuǎn)成wav上傳
1.安裝js-audio-recorder插件并引入
npm i js-audio-recorder import Recorder from "js-audio-recorder"; // 錄音插件
2.頁面錄音彈窗
<div class="voiceCss" v-if='voiceShow'> <div class="voiceButton"> <el-button class="buttonCss" type="primary"> <span v-if="voiceObj.type" @click="voiceType('start')">開始錄制</span> <span v-else @click="voiceType('end')">結(jié)束錄制</span> </el-button> </div> <div class="voiceCanvas"> <h3>錄音時(shí)長(zhǎng):{{ recorder.duration.toFixed(4) }}</h3> <div style="width:100%;height:200px;border:1px solid red;margin-top: 5px;"> <canvas id="canvas" style="width: 100%;height: 100%;"></canvas> </div> </div> </div>
3.變量
data() { return { voiceShow: false, // 錄音彈窗是否顯示 voiceObj:{ type:true, // true-開始錄音 false-結(jié)束錄音 state:true, // true-恢復(fù)錄音 false-暫停錄音 }, recorder: new Recorder({ sampleBits: 16, // 采樣位數(shù),支持 8 或 16,默認(rèn)是16 sampleRate: 16000, // 采樣率,支持 11025、16000、22050、24000、44100、48000,根據(jù)瀏覽器默認(rèn)值,我的chrome是48000 numChannels: 1, // 聲道,支持 1 或 2, 默認(rèn)是1 // compiling: false,(0.x版本中生效,1.x增加中) // 是否邊錄邊轉(zhuǎn)換,默認(rèn)是false }), //波浪圖-錄音 drawRecordId:null, oCanvas : null, ctx : null, } }
4.打開錄音面板
voiceFun(){ // 打開錄音彈窗 this.voiceShow = true; // 初始化按鈕狀態(tài)(開始錄制/結(jié)束錄制) this.voiceObj = { type:true, state:true, }; setTimeout(()=>{ // 錄音波浪元素 this.oCanvas = document.getElementById('canvas'); this.ctx = this.oCanvas.getContext("2d"); },100) },
5.開始結(jié)束錄音
voiceType(index){ if(index=='start'){ // 開始錄音 let that = this; Recorder.getPermission().then( () => { // console.log("開始錄音"); that.recorder.start(); // 開始錄音 that.drawRecord(); //開始繪制圖片 that.voiceObj.type = false; }, (error) => { that.$message({ message: "請(qǐng)先允許該網(wǎng)頁使用麥克風(fēng)", type: "info", }); console.log(`${error.name} : ${error.message}`); } ); }else if(index=='end') { // 結(jié)束錄音 this.recorder.stop(); this.voiceObj.type = true; // 獲取錄音文件 // this.getVoiceWAV(); // 上傳錄音文件 this.uploadvoiceWAV(); } },
6.繪制波浪圖
drawRecord () { // 用requestAnimationFrame穩(wěn)定60fps繪制 this.drawRecordId = requestAnimationFrame(this.drawRecord); // 實(shí)時(shí)獲取音頻大小數(shù)據(jù) let dataArray = this.recorder.getRecordAnalyseData(), bufferLength = dataArray.length; // 填充背景色 this.ctx.fillStyle = 'rgb(200, 200, 200)'; this.ctx.fillRect(0, 0, this.oCanvas.width, this.oCanvas.height); // 設(shè)定波形繪制顏色 this.ctx.lineWidth = 2; this.ctx.strokeStyle = 'rgb(0, 0, 0)'; this.ctx.beginPath(); var sliceWidth = this.oCanvas.width * 1.0 / bufferLength, // 一個(gè)點(diǎn)占多少位置,共有bufferLength個(gè)點(diǎn)要繪制 x = 0; // 繪制點(diǎn)的x軸位置 for (var i = 0; i < bufferLength; i++) { var v = dataArray[i] / 128.0; var y = v * this.oCanvas.height / 2; if (i === 0) { // 第一個(gè)點(diǎn) this.ctx.moveTo(x, y); } else { // 剩余的點(diǎn) this.ctx.lineTo(x, y); } // 依次平移,繪制所有點(diǎn) x += sliceWidth; } this.ctx.lineTo(this.oCanvas.width, this.oCanvas.height / 2); this.ctx.stroke(); },
7.上傳錄音文件
uploadvoiceWAV(){ let dom = document.querySelector(".voiceBox") const loading = this.$loading({ lock: true, target: dom, text: '請(qǐng)稍等,正在語音轉(zhuǎn)文字', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)' }); let wavBlob = this.recorder.getWAVBlob(); // 創(chuàng)建一個(gè)formData對(duì)象 var formData = new FormData() // 此處獲取到blob對(duì)象后需要設(shè)置fileName滿足當(dāng)前項(xiàng)目上傳需求,其它項(xiàng)目可直接傳把blob作為file塞入formData const newbolb = new Blob([wavBlob], { type: 'audio/wav' }) //獲取當(dāng)時(shí)時(shí)間戳作為文件名 const fileOfBlob = new File([newbolb], new Date().getTime() + '.wav') formData.append('file', fileOfBlob) uploadWavData(formData).then((response) => { // uploadWavData替換成自己的接口引入 loading.close(); if(response.code==200){ this.voiceShow = false; // 上傳成功,并轉(zhuǎn)文字 }else if(response.code==500){ this.$message({ message: response.msg+"請(qǐng)重試", type: "error", }); } }); },
8.獲取錄音文件(WAV)
getVoiceWAV(){ let wavBlob = this.recorder.getWAVBlob(); console.log(wavBlob); },
總結(jié)
到此這篇關(guān)于Vue如何使用js-audio-recorder插件實(shí)現(xiàn)錄音功能并將文件轉(zhuǎn)成wav上傳的文章就介紹到這了,更多相關(guān)Vue錄音并將文件轉(zhuǎn)成wav上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE多個(gè)下拉框?qū)崿F(xiàn)雙向聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了VUE多個(gè)下拉框?qū)崿F(xiàn)雙向聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07vue 頁面跳轉(zhuǎn)的實(shí)現(xiàn)方式
這篇文章主要介紹了vue 頁面跳轉(zhuǎn)的實(shí)現(xiàn)方式,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-01-01vue實(shí)現(xiàn)3D切換滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)偽3D切換滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04詳解Vue中雙向綁定原理及簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Vue中雙向綁定原理及簡(jiǎn)單實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們深入了解Vue有一定的幫助,需要的可以參考一下2023-05-05vue中 el-table每個(gè)單元格包含多個(gè)數(shù)據(jù)項(xiàng)處理
vue項(xiàng)目中,我們需要在el-table中顯示數(shù)組數(shù)據(jù),有的時(shí)候,需要在一個(gè)單元格中顯示多條數(shù)據(jù),如何實(shí)現(xiàn)呢,對(duì)vue el-table單元格相關(guān)知識(shí)感興趣的朋友一起看看吧2023-11-11VueCLI通過process.env配置環(huán)境變量的實(shí)現(xiàn)
本文主要介紹了VueCLI通過process.env配置環(huán)境變量的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07vue+element ui實(shí)現(xiàn)錨點(diǎn)定位
這篇文章主要為大家詳細(xì)介紹了vue+element ui實(shí)現(xiàn)錨點(diǎn)定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06vue+element_ui上傳文件,并傳遞額外參數(shù)操作
這篇文章主要介紹了vue+element_ui上傳文件,并傳遞額外參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12