vue使用js-audio-recorder實現(xiàn)錄音功能
前言
最近項目中需要實現(xiàn)一個錄音上傳功能,用于語音評論可以上錄音。
下載插件:
npm i js-audio-recorder
完整代碼
<template> <div style="padding: 20px;"> <h3>錄音上傳</h3> <div style="font-size:14px"> <h3>錄音時長:{{ recorder && recorder.duration.toFixed(4) }}</h3> <br /> <el-button type="primary" @click="handleStart">開始錄音</el-button> <el-button type="info" @click="handlePause">暫停錄音</el-button> <el-button type="success" @click="handleResume">繼續(xù)錄音</el-button> <el-button type="warning" @click="handleStop">停止錄音</el-button> <br /> <br /> <h3> 播放時長:{{ recorder && (playTime > recorder.duration ? recorder.duration.toFixed(4) : playTime.toFixed(4)) }} </h3> <br /> <el-button type="primary" @click="handlePlay">播放錄音</el-button> <el-button type="info" @click="handlePausePlay">暫停播放</el-button> <el-button type="success" @click="handleResumePlay">繼續(xù)播放</el-button> <el-button type="warning" @click="handleStopPlay">停止播放</el-button> <el-button type="error" @click="handleDestroy">銷毀錄音</el-button> <el-button type="primary" @click="uploadRecord">上傳</el-button> </div> </div> </template> <script> import Recorder from 'js-audio-recorder' export default { data() { return { recorder: null, playTime: 0, timer: null, src: null } }, created() { this.recorder = new Recorder() }, methods: { // 開始錄音 handleStart() { this.recorder = new Recorder() Recorder.getPermission().then(() => { console.log('開始錄音') this.recorder.start() // 開始錄音 }, (error) => { this.$message({ message: '請先允許該網(wǎng)頁使用麥克風', type: 'info' }) console.log(`${error.name} : ${error.message}`) }) }, handlePause() { console.log('暫停錄音') this.recorder.pause() // 暫停錄音 }, handleResume() { console.log('恢復錄音') this.recorder.resume() // 恢復錄音 }, handleStop() { console.log('停止錄音') this.recorder.stop() // 停止錄音 }, handlePlay() { console.log('播放錄音') console.log(this.recorder) this.recorder.play() // 播放錄音 // 播放時長 this.timer = setInterval(() => { try { this.playTime = this.recorder.getPlayTime() } catch (error) { this.timer = null } }, 100) }, handlePausePlay() { console.log('暫停播放') this.recorder.pausePlay() // 暫停播放 // 播放時長 this.playTime = this.recorder.getPlayTime() this.time = null }, handleResumePlay() { console.log('恢復播放') this.recorder.resumePlay() // 恢復播放 // 播放時長 this.timer = setInterval(() => { try { this.playTime = this.recorder.getPlayTime() } catch (error) { this.timer = null } }, 100) }, handleStopPlay() { console.log('停止播放') this.recorder.stopPlay() // 停止播放 // 播放時長 this.playTime = this.recorder.getPlayTime() this.timer = null }, handleDestroy() { console.log('銷毀實例') this.recorder.destroy() // 毀實例 this.timer = null }, uploadRecord() { if (this.recorder == null || this.recorder.duration === 0) { this.$message({ message: '請先錄音', type: 'error' }) return false } this.recorder.pause() // 暫停錄音 this.timer = null console.log('上傳錄音')// 上傳錄音 const formData = new FormData() const blob = this.recorder.getWAVBlob()// 獲取wav格式音頻數(shù)據(jù) // 此處獲取到blob對象后需要設置fileName滿足當前項目上傳需求,其它項目可直接傳把blob作為file塞入formData const newbolb = new Blob([blob], { type: 'audio/wav' }) const fileOfBlob = new File([newbolb], new Date().getTime() + '.wav') formData.append('file', fileOfBlob) const url = window.URL.createObjectURL(fileOfBlob) this.src = url // const axios = require('axios') // axios.post(url, formData).then(res => { // console.log(res.data.data[0].url) // }) } } } </script>
播放通過audio標簽控制,可以上傳到公司服務器獲取線上地址,還可以通過blob對象獲取到播放url
const blob = this.recorder.getWAVBlob() this.url = window.URL.createObjectURL(blob)
注意事項
注意,調(diào)試環(huán)境這里會報錯,所以開始解決報錯問題:
報錯:error:瀏覽器不支持getUserMedia !
其實這是因為瀏覽器不支持http:IP開頭的路徑,認為這個路徑不安全
瀏覽器只支持file:,https:,http://localhost,
解決辦法:
chrome瀏覽器
地址欄輸入:chrome://flags/#unsafely-treat-insecure-origin-as-secure
輸入你的本地網(wǎng)址,改為enabled,選擇重啟瀏覽器按鈕【生產(chǎn)環(huán)境當中由于是使用域名進行訪問,所以就不會報錯?!?/p>
然后就OK了
總結(jié)
1.錄音時長duration是recorder的屬性,可以實時獲取;但播放時長需要通過方法getPlayTime()獲取,播放時不能實時改變,此處我用了個100ms延遲的定時器假裝實時獲取,如果有更好的辦法,歡迎指教。
2.getWAVBlob()獲取錄音數(shù)據(jù)的方法獲取的時blob對象,當前項目中需要驗證fileName,所以需要把blob轉(zhuǎn)成file,改變fileName上傳。
3.官網(wǎng)提供的demo中還有波形圖,可以參考。
官網(wǎng)地址:https://recorder-api.zhuyuntao.cn/
官網(wǎng)項目演示地址:https://recorder.zhuyuntao.cn/
更新
客戶說錄音文件太大,20s就有2M左右,需要壓縮。
查看文檔降低采樣位數(shù),采樣率,單聲道可以降低音頻質(zhì)量,測試20s大概只有200k左右,只需獲取record對象時申明即可。
this.recorder = new Recorder({ sampleBits: 8, // 采樣位數(shù),支持 8 或 16,默認是16 sampleRate: 11025, // 采樣率,支持 11025、16000、22050、24000、44100、48000,根據(jù)瀏覽器默認值,我的chrome是48000 numChannels: 1 // 聲道,支持 1 或 2, 默認是1 // compiling: false,(0.x版本中生效,1.x增加中) // 是否邊錄邊轉(zhuǎn)換,默認是false })
到此這篇關于vue使用js-audio-recorder實現(xiàn)錄音功能的文章就介紹到這了,更多相關vue錄音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue 實現(xiàn)數(shù)字滾動增加效果的實例代碼
最近做了個項目需要做數(shù)字滾動增加的效果,剛開始接到這個項目還真是懵了,后來發(fā)現(xiàn)實現(xiàn)代碼很簡單的,下面小編給大家?guī)砹藇ue 實現(xiàn)數(shù)字滾動增加效果的實例代碼,需要的朋友參考下吧2018-07-07vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐
本文主要介紹了vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11