Vue 使用js-audio-recorder插件实现录音功能并将文件转成wav上传

在这里插入图片描述

1.安装js-audio-recorder插件并引入

npm i js-audio-recorder
import Recorder from "js-audio-recorder";   // 录音插件

2.页面录音弹窗

<div class="voiceCss" v-if='voiceShow'>
  <div class="voiceButton"> 
     <el-button class="buttonCss" type="primary">
        <span v-if="voiceObj.type" @click="voiceType('start')">开始录制</span>
        <span v-else @click="voiceType('end')">结束录制</span>
     </el-button>
  </div>
  <div class="voiceCanvas">
     <h3>录音时长:{{ recorder.duration.toFixed(4) }}</h3>
     <div style="width:100%;height:200px;border:1px solid red;margin-top: 5px;">
        <canvas id="canvas" style="width: 100%;height: 100%;"></canvas>
     </div>
  </div>
</div>

3.变量

data() {
    return {
      voiceShow: false,  // 录音弹窗是否显示
      voiceObj:{
        type:true,   // true-开始录音  false-结束录音
        state:true,  // true-恢复录音  false-暂停录音
      },
      recorder: new Recorder({
        sampleBits: 16,    // 采样位数,支持 8 或 16,默认是16
        sampleRate: 16000, // 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000
        numChannels: 1,   // 声道,支持 1 或 2, 默认是1
        // compiling: false,(0.x版本中生效,1.x增加中)  // 是否边录边转换,默认是false
     }),
     //波浪图-录音
     drawRecordId:null,
     oCanvas : null,
     ctx : null,
  }
}

4.打开录音面板

voiceFun(){
  // 打开录音弹窗
  this.voiceShow = true;
  // 初始化按钮状态(开始录制/结束录制)
  this.voiceObj = {
    type:true,
    state:true,
  };
  setTimeout(()=>{
  	// 录音波浪元素
    this.oCanvas = document.getElementById('canvas');
    this.ctx = this.oCanvas.getContext("2d");
  },100)
},

5.开始结束录音

voiceType(index){
  if(index=='start'){  // 开始录音
    let that = this;
    Recorder.getPermission().then(
      () => {
        // console.log("开始录音");
        that.recorder.start(); // 开始录音
        that.drawRecord();  //开始绘制图片
        that.voiceObj.type = false;
      },
      (error) => {
        that.$message({
          message: "请先允许该网页使用麦克风",
          type: "info",
        });
        console.log(`${error.name} : ${error.message}`);
      }
    );
  }else if(index=='end') {  // 结束录音
     this.recorder.stop();
     this.voiceObj.type = true;
     // 获取录音文件
     // this.getVoiceWAV();
     // 上传录音文件
     this.uploadvoiceWAV();
  }
},

6.绘制波浪图

drawRecord () {
  // 用requestAnimationFrame稳定60fps绘制
  this.drawRecordId = requestAnimationFrame(this.drawRecord);
  // 实时获取音频大小数据
  let dataArray = this.recorder.getRecordAnalyseData(),
      bufferLength = dataArray.length;
  // 填充背景色
  this.ctx.fillStyle = 'rgb(200, 200, 200)';
  this.ctx.fillRect(0, 0, this.oCanvas.width, this.oCanvas.height);
  // 设定波形绘制颜色
  this.ctx.lineWidth = 2;
  this.ctx.strokeStyle = 'rgb(0, 0, 0)';
  this.ctx.beginPath();
  var sliceWidth = this.oCanvas.width * 1.0 / bufferLength, // 一个点占多少位置,共有bufferLength个点要绘制
  x = 0;          // 绘制点的x轴位置
  for (var i = 0; i < bufferLength; i++) {
    var v = dataArray[i] / 128.0;
    var y = v * this.oCanvas.height / 2;
    if (i === 0) {
      // 第一个点
      this.ctx.moveTo(x, y);
    } else {
      // 剩余的点
      this.ctx.lineTo(x, y);
    }
    // 依次平移,绘制所有点
    x += sliceWidth;
  }
  this.ctx.lineTo(this.oCanvas.width, this.oCanvas.height / 2);
  this.ctx.stroke();
},

7.上传录音文件

uploadvoiceWAV(){
  let dom = document.querySelector(".voiceBox")
  const loading = this.$loading({
    lock: true,
    target: dom,
    text: '请稍等,正在语音转文字',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
  let wavBlob = this.recorder.getWAVBlob();
  // 创建一个formData对象
  var formData = new FormData()
  // 此处获取到blob对象后需要设置fileName满足当前项目上传需求,其它项目可直接传把blob作为file塞入formData
  const newbolb = new Blob([wavBlob], { type: 'audio/wav' })
  //获取当时时间戳作为文件名
  const fileOfBlob = new File([newbolb], new Date().getTime() + '.wav')
  formData.append('file', fileOfBlob)
  uploadWavData(formData).then((response) => {  // uploadWavData替换成自己的接口引入
    loading.close();
    if(response.code==200){
       this.voiceShow = false;
       // 上传成功,并转文字
    }else if(response.code==500){
      this.$message({
        message: response.msg+"请重试",
        type: "error",
      });
    }
  });
},

8.获取录音文件(WAV)

getVoiceWAV(){
  let wavBlob = this.recorder.getWAVBlob();
  console.log(wavBlob);
},
Vivado2023是一款集成开发环境软件,用于设计和验证FPGA(现场可编程门阵列)和可编程逻辑器件。对于使用Vivado2023的用户来说,license是必不可少的。 Vivado2023的license是一种许可证,用于授权用户合法使用该软件。许可证分为多种类型,包括评估许可证、开发许可证和节点许可证等。每种许可证都有不同的使用条件和功能。 评估许可证是免费提供的,让用户可以在一段时间内试用Vivado2023的全部功能。用户可以使用这个许可证来了解软件的性能和特点,对于初学者和小规模项目来说是一个很好的选择。但是,使用评估许可证的用户在使用期限过后需要购买正式的许可证才能继续使用软件。 开发许可证是付费的,可以永久使用Vivado2023的全部功能。这种许可证适用于需要长期使用Vivado2023进行开发的用户,通常是专业的FPGA设计师或工程师。购买开发许可证可以享受Vivado2023的技术支持和更新服务,确保软件始终保持最新的版本和功能。 节点许可证是用于多设备或分布式设计的许可证,可以在多个计算机上安装Vivado2023,并共享使用。节点许可证适用于大规模项目或需要多个处理节点进行设计的用户,可以提高工作效率和资源利用率。 总之,Vivado2023 license是用户在使用Vivado2023时必须考虑的问题。用户可以根据自己的需求选择合适的许可证类型,以便获取最佳的软件使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值