關(guān)于vue使用recorder.js錄音功能,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),城北企業(yè)網(wǎng)站建設(shè),城北品牌網(wǎng)站建設(shè),網(wǎng)站定制,城北網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,城北網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
**
1, 引入外部js文件
import { HZRecorder} from ‘…/…/utils/HZRecorder.js';
js文件內(nèi)容
export function HZRecorder(stream, config) { config = config || {}; config.sampleBits = config.sampleBits || 16; //采樣數(shù)位 8, 16 config.sampleRate = config.sampleRate || 16000; //采樣率16khz var context = new (window.webkitAudioContext || window.AudioContext)(); var audioInput = context.createMediaStreamSource(stream); var createScript = context.createScriptProcessor || context.createJavaScriptNode; var recorder = createScript.apply(context, [4096, 1, 1]); var audioData = { size: 0 //錄音文件長(zhǎng)度 , buffer: [] //錄音緩存 , inputSampleRate: context.sampleRate //輸入采樣率 , inputSampleBits: 16 //輸入采樣數(shù)位 8, 16 , outputSampleRate: config.sampleRate //輸出采樣率 , oututSampleBits: config.sampleBits //輸出采樣數(shù)位 8, 16 , input: function (data) { this.buffer.push(new Float32Array(data)); this.size += data.length; } , compress: function () { //合并壓縮 //合并 var data = new Float32Array(this.size); var offset = 0; for (var i = 0; i < this.buffer.length; i++) { data.set(this.buffer[i], offset); offset += this.buffer[i].length; } //壓縮 var compression = parseInt(this.inputSampleRate / this.outputSampleRate); var length = data.length / compression; var result = new Float32Array(length); var index = 0, j = 0; while (index < length) { result[index] = data[j]; j += compression; index++; } return result; } , encodeWAV: function () { var sampleRate = Math.min(this.inputSampleRate, this.outputSampleRate); var sampleBits = Math.min(this.inputSampleBits, this.oututSampleBits); var bytes = this.compress(); var dataLength = bytes.length * (sampleBits / 8); var buffer = new ArrayBuffer(44 + dataLength); var data = new DataView(buffer); var channelCount = 1;//單聲道 var offset = 0; var writeString = function (str) { for (var i = 0; i < str.length; i++) { data.setUint8(offset + i, str.charCodeAt(i)); } } // 資源交換文件標(biāo)識(shí)符 writeString('RIFF'); offset += 4; // 下個(gè)地址開(kāi)始到文件尾總字節(jié)數(shù),即文件大小-8 data.setUint32(offset, 36 + dataLength, true); offset += 4; // WAV文件標(biāo)志 writeString('WAVE'); offset += 4; // 波形格式標(biāo)志 writeString('fmt '); offset += 4; // 過(guò)濾字節(jié),一般為 0x10 = 16 data.setUint32(offset, 16, true); offset += 4; // 格式類(lèi)別 (PCM形式采樣數(shù)據(jù)) data.setUint16(offset, 1, true); offset += 2; // 通道數(shù) data.setUint16(offset, channelCount, true); offset += 2; // 采樣率,每秒樣本數(shù),表示每個(gè)通道的播放速度 data.setUint32(offset, sampleRate, true); offset += 4; // 波形數(shù)據(jù)傳輸率 (每秒平均字節(jié)數(shù)) 單聲道×每秒數(shù)據(jù)位數(shù)×每樣本數(shù)據(jù)位/8 data.setUint32(offset, channelCount * sampleRate * (sampleBits / 8), true); offset += 4; // 快數(shù)據(jù)調(diào)整數(shù) 采樣一次占用字節(jié)數(shù) 單聲道×每樣本的數(shù)據(jù)位數(shù)/8 data.setUint16(offset, channelCount * (sampleBits / 8), true); offset += 2; // 每樣本數(shù)據(jù)位數(shù) data.setUint16(offset, sampleBits, true); offset += 2; // 數(shù)據(jù)標(biāo)識(shí)符 writeString('data'); offset += 4; // 采樣數(shù)據(jù)總數(shù),即數(shù)據(jù)總大小-44 data.setUint32(offset, dataLength, true); offset += 4; // 寫(xiě)入采樣數(shù)據(jù) if (sampleBits === 8) { for (var i = 0; i < bytes.length; i++, offset++) { var s = Math.max(-1, Math.min(1, bytes[i])); var val = s < 0 ? s * 0x8000 : s * 0x7FFF; val = parseInt(255 / (65535 / (val + 32768))); data.setInt8(offset, val, true); } } else { for (var i = 0; i < bytes.length; i++, offset += 2) { var s = Math.max(-1, Math.min(1, bytes[i])); data.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true); } } return new Blob([data], { type: 'audio/wav' }); } }; //開(kāi)始錄音 this.start = function () { audioInput.connect(recorder); recorder.connect(context.destination); } //停止 this.stop = function () { recorder.disconnect(); } //獲取音頻文件 this.getBlob = function () { this.stop(); return audioData.encodeWAV(); } //回放 this.play = function (audio) { var blob=this.getBlob(); // saveAs(blob, "F:/3.wav"); audio.src = window.URL.createObjectURL(this.getBlob()); } //上傳 this.upload = function () { return this.getBlob() } //音頻采集 recorder.onaudioprocess = function (e) { audioData.input(e.inputBuffer.getChannelData(0)); //record(e.inputBuffer.getChannelData(0)); } }
2、vue組件的mount中初始化調(diào)用麥克風(fēng)工具
mounted() { this.$nextTick(() => { try { window.AudioContext = window.AudioContext || window.webkitAudioContext; navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; window.URL = window.URL || window.webkitURL; audio_context = new AudioContext; console.log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!')); } catch (e) { alert('No web audio support in this browser!'); } navigator.getUserMedia({audio: true}, function (stream) { recorder = new HZRecorder(stream) console.log('初始化完成'); }, function(e) { console.log('No live audio input: ' + e); }); }) },
3、methods 調(diào)用
readyOriginal () { if (!this.isVoice) { recorder && recorder.start(); this.isVoice = true } else { this.isVoice = false recorder && recorder.stop(); setTimeout(()=> { var mp3Blob = recorder.upload(); var fd = new FormData(); fd.append('audio', mp3Blob); this.$http({ header: ({ 'Content-Type': 'application/x-www-form-urlencodeed' }), method: 'POST', url: 'url', data: fd, withCredentials: true, }).then((res) => { // 這里做登錄攔截 if (res.data.isLogin === false) { router.replace('/login'); } else { if (res.data.status === 200) { console.log('保存成功') } else { this.returnmsg = '上傳失敗' } } }) },1000) } },
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。