1. 程式人生 > 實用技巧 >vue 使用瀏覽器實現錄音功能

vue 使用瀏覽器實現錄音功能

在專案vue中遇到使用瀏覽器實現錄音功能

要在https下才能實現! 要在https下才能實現!! 要在https下才能實現!!!

如果不是https,解決方案https://www.cnblogs.com/Sabo-dudu/p/12449985.html

注意: 這裡輸入的檔案格式為mp3,因為wav格式的檔案太大了。

專案是基於 vue-cli3 的pc端應用

1. 下載檔案,下載完成後把它放在 根目錄 public 資料夾下。

2. 然後在 index.html 中全域性引入

    <script type="text/javascript" src="/recorder/js/recorder.js"></script>

3. 建立元件mRecorder.vue

<template>
  <div class="wrap-recorder">
    <el-row>
      <el-col>
        <svg
          @click="handleClick"
          :class="['icon', {anirecorder: recording } ]"
          t="1581399509621"
          viewBox="0 0 1024 1024"
          version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="3242" width="50" height="50" > <path d="M512 1024a512 512 0 1 1 512-512 512 512 0 0 1-512 512z m0-992a480 480 0 1 0 480 480A480 480 0 0 0 512 32z m16 623.2V736h48a16 16 0 0 1 0 32h-128a16 16 0 0 1 0-32h48v-80.8A160 160 0 0 1 352 496a16 16 0 0 1 32 0 128 128 0 0 0 256 0 16 16 0 0 1 32 0 160 160 0 0 1-144 159.2zM512 592a96 96 0 0 1-96-96v-144a96 96 0 0 1 192 0v144a96 96 0 0 1-96 96z m64-240a64 64 0 0 0-128 0v144a64 64 0 0 0 128 0v-144z"
p-id="3243" fill="#707070" /> </svg> </el-col> </el-row> <p class="tip">{{ tiptext }}</p> </div> </template> <script> export default { data() { return { tiptext: "點選錄音", recording: false, // 標記是否在錄音 intervaltimerid: "", // 間隔時間定時器編號 recorder: null, time: 0 }; }, mounted() { this.init(); }, methods: { init() { this.recorder = new MP3Recorder({ //numChannels: 1, //聲道數,預設為1 //sampleRate: 8000, //取樣率,一般由裝置提供,比如 48000 bitRate: 128, //位元率,不要低於64,否則可能錄製無聲音(人聲) //useMediaRecorder: true, //是否使用MediaRecorder錄音(不支援轉碼為mp3檔案) //recorderType: "video/webm;codes=vp9", //預設編碼,僅 useMediaRecorder 為true且瀏覽器支援時生效 //錄音結束事件 complete: (blob, ext) => { var url = URL.createObjectURL(blob); this.$emit("handleStop", { url: url, mblob: blob }); } }); }, // 點選處理 handleClick() { //錄音支援檢測 if (!this.recorder.support) return; this.recording = !this.recording; this.$emit("handleStop", { url: "", mblob: null, }); if (this.recording) { this.time = 0; this.tiptext = "錄音中 " + this.time + "s"; this.recorder.start(this.successFun(), this.errorFun()); } else { clearInterval(this.intervaltimerid); this.recorder.stop(); this.tiptext = "點選錄音"; } }, writeError() {}, successFun() { this.intervaltimerid = setInterval(() => { // 開始累積 if (this.time == 120) { this.recorder.stop(); this.recording = false; this.tiptext = "點選錄音"; this.$message.warning("對不起,錄音只支援120秒以內的語音!"); clearInterval(this.intervaltimerid); return false; } this.time = this.time + 1; this.tiptext = "錄音中 " + this.time + "s"; }, 1000); }, errorFun(e) { // clearInterval(this.intervaltimerid); // switch (e.code || e.name) { // case "PERMISSION_DENIED": // case "PermissionDeniedError": // // this.writeError("使用者拒絕提供裝置。"); // break; // case "NOT_SUPPORTED_ERROR": // case "NotSupportedError": // // this.writeError("瀏覽器不支援硬體裝置。"); // break; // case "MANDATORY_UNSATISFIED_ERROR": // case "MandatoryUnsatisfiedError": // // this.writeError("無法發現指定的硬體裝置。"); // break; // default: // // this.writeError( // // "無法開啟麥克風。異常資訊:" + (e.code || e.name) // // ); // break; // } } } }; </script> <style scoped lang="scss"> .wrap-recorder { text-align: center; .icon { cursor: pointer; } .tip { padding-top: 20px; } } .anirecorder { position: relative; animation: mymove 5s infinite; animation-direction: alternate; animation-timing-function: ease-in-out; } @keyframes mymove { 0% { transform: scale(1); /*開始為原始大小*/ } 25% { transform: scale(1.1); /*放大1.1倍*/ } 50% { transform: scale(0.9); } 75% { transform: scale(1.1); } } </style>
View Code

4. 然後引入該元件

 <MRecorder @handleStop="handelEndRecord" />

methods:{
  // 錄音處理結束事件
    handelEndRecord(param) {
      this.msource = param;
    },
}

整個流程就完成了,生成的mp3 格式很小,便於上傳。