欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue使用recorder.js實(shí)現(xiàn)錄音功能

 更新時(shí)間:2019年11月22日 15:44:06   作者:qq_34707038  
這篇文章主要為大家詳細(xì)介紹了vue使用recorder.js實(shí)現(xiàn)錄音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

關(guān)于vue使用recorder.js錄音功能,供大家參考,具體內(nèi)容如下

**

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è)地址開始到文件尾總字節(jié)數(shù),即文件大小-8 
      data.setUint32(offset, 36 + dataLength, true); offset += 4;
      // WAV文件標(biāo)志
      writeString('WAVE'); offset += 4;
      // 波形格式標(biāo)志 
      writeString('fmt '); offset += 4;
      // 過濾字節(jié),一般為 0x10 = 16 
      data.setUint32(offset, 16, true); offset += 4;
      // 格式類別 (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;
      // 寫入采樣數(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' });
    }
  };
  //開始錄音
  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 {
 <!-- 檢查是否能夠調(diào)用麥克風(fēng) -->
 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
  <!-- 結(jié)束錄音 -->
  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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于Elementui中toggleRowSelection()方法實(shí)現(xiàn)分頁(yè)切換時(shí)記錄之前選中的狀態(tài)

    關(guān)于Elementui中toggleRowSelection()方法實(shí)現(xiàn)分頁(yè)切換時(shí)記錄之前選中的狀態(tài)

    這篇文章主要介紹了關(guān)于Elementui中toggleRowSelection()方法實(shí)現(xiàn)分頁(yè)切換時(shí)記錄之前選中的狀態(tài),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue3之路由的query參數(shù)和params參數(shù)用法

    Vue3之路由的query參數(shù)和params參數(shù)用法

    這篇文章主要介紹了Vue3之路由的query參數(shù)和params參數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue-router:嵌套路由的使用方法

    vue-router:嵌套路由的使用方法

    本篇文章主要介紹了vue-router:嵌套路由的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Vue.js watch監(jiān)視屬性知識(shí)點(diǎn)總結(jié)

    Vue.js watch監(jiān)視屬性知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于Vue.js watch監(jiān)視屬性的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • vuex存儲(chǔ)數(shù)據(jù)的幾種方法實(shí)例詳解

    vuex存儲(chǔ)數(shù)據(jù)的幾種方法實(shí)例詳解

    在瀏覽網(wǎng)頁(yè)時(shí)我們有些時(shí)候需要記住一些用戶選擇的信息,比如登陸時(shí)我們?nèi)绻x擇了記住密碼,那么我們下次進(jìn)入該網(wǎng)頁(yè)時(shí)就會(huì)有你上次的登陸信息,下面這篇文章主要給大家介紹了關(guān)于vuex存儲(chǔ)數(shù)據(jù)的幾種方法,需要的朋友可以參考下
    2022-10-10
  • 使用Vue逐步實(shí)現(xiàn)Watch屬性詳解

    使用Vue逐步實(shí)現(xiàn)Watch屬性詳解

    這篇文章主要介紹了使用Vue逐步實(shí)現(xiàn)Watch屬性詳解,watch對(duì)象中的value分別支持函數(shù)、數(shù)組、字符串、對(duì)象,較為常用的是函數(shù)的方式,當(dāng)想要觀察一個(gè)對(duì)象以及對(duì)象中的每一個(gè)屬性的變化時(shí),便會(huì)用到對(duì)象的方式
    2022-08-08
  • v-distpicker地區(qū)選擇器組件使用實(shí)例詳解

    v-distpicker地區(qū)選擇器組件使用實(shí)例詳解

    代碼添加了一個(gè)vDistpickerHandle的事件處理函數(shù)對(duì)地區(qū)選擇器中的數(shù)據(jù)進(jìn)行處理,將數(shù)據(jù)存儲(chǔ)到form對(duì)象的相應(yīng)屬性中,方便數(shù)據(jù)提交,這篇文章主要介紹了v-distpicker地區(qū)選擇器組件使用,需要的朋友可以參考下
    2024-02-02
  • vue中各種通信傳值方式總結(jié)

    vue中各種通信傳值方式總結(jié)

    在本篇文章里小編給大家分享了關(guān)于vue中各種通信傳值方式的相關(guān)知識(shí)點(diǎn),有興趣的朋友們學(xué)習(xí)下。
    2019-02-02
  • vue全局自定義指令-元素拖拽的實(shí)現(xiàn)代碼

    vue全局自定義指令-元素拖拽的實(shí)現(xiàn)代碼

    這篇文章主要介紹了面板拖拽之vue自定義指令,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • 功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用

    功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用

    這篇文章主要為大家介紹了功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06

最新評(píng)論