您好,登錄后才能下訂單哦!
關于vue使用recorder.js錄音功能,供大家參考,具體內容如下
**
1, 引入外部js文件
import { HZRecorder} from ‘…/…/utils/HZRecorder.js';
js文件內容
export function HZRecorder(stream, config) { config = config || {}; config.sampleBits = config.sampleBits || 16; //采樣數位 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 //錄音文件長度 , buffer: [] //錄音緩存 , inputSampleRate: context.sampleRate //輸入采樣率 , inputSampleBits: 16 //輸入采樣數位 8, 16 , outputSampleRate: config.sampleRate //輸出采樣率 , oututSampleBits: config.sampleBits //輸出采樣數位 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)); } } // 資源交換文件標識符 writeString('RIFF'); offset += 4; // 下個地址開始到文件尾總字節數,即文件大小-8 data.setUint32(offset, 36 + dataLength, true); offset += 4; // WAV文件標志 writeString('WAVE'); offset += 4; // 波形格式標志 writeString('fmt '); offset += 4; // 過濾字節,一般為 0x10 = 16 data.setUint32(offset, 16, true); offset += 4; // 格式類別 (PCM形式采樣數據) data.setUint16(offset, 1, true); offset += 2; // 通道數 data.setUint16(offset, channelCount, true); offset += 2; // 采樣率,每秒樣本數,表示每個通道的播放速度 data.setUint32(offset, sampleRate, true); offset += 4; // 波形數據傳輸率 (每秒平均字節數) 單聲道×每秒數據位數×每樣本數據位/8 data.setUint32(offset, channelCount * sampleRate * (sampleBits / 8), true); offset += 4; // 快數據調整數 采樣一次占用字節數 單聲道×每樣本的數據位數/8 data.setUint16(offset, channelCount * (sampleBits / 8), true); offset += 2; // 每樣本數據位數 data.setUint16(offset, sampleBits, true); offset += 2; // 數據標識符 writeString('data'); offset += 4; // 采樣數據總數,即數據總大小-44 data.setUint32(offset, dataLength, true); offset += 4; // 寫入采樣數據 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' }); } }; //開始錄音 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中初始化調用麥克風工具
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 調用
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) } },
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。