vue实现录音功能js-audio-recorder带波浪图效果的示例

yizhihongxing

当需要在Vue中展示录音并且需要带有波浪效果时,我们可以使用js-audio-recorder这个JavaScript库。下面将详细讲解如何在Vue中使用js-audio-recorder来实现录音功能,并带有波浪图效果的示例。

准备工作

在开始之前,我们需要进行准备工作:

  1. 在Vue项目中安装js-audio-recorder
    npm install js-audio-recorder --save

  2. 在main.js中全局引入js-audio-recorder

import AudioRecorder from 'js-audio-recorder';
window.AudioRecorder = AudioRecorder;

准备工作完成之后,我们就可以在Vue组件中使用js-audio-recorder来实现录音功能。

示例1 - 基础录音

下面是一个基础的录音示例,可以录制10秒钟的音频,录音完成后,可以通过播放按钮来播放录制的音频。

<template>
  <div>
    <div>
      <button @click="startRecording">开始录音</button>
      <button @click="stopRecording">停止录音</button>
    </div>
    <div>
      <button @click="playRecordedAudio">播放录制的音频</button>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        audioRecorder: null,
        recording: false,
        recordedAudioBlob: null
      }
    },
    mounted() {
      this.audioRecorder = new AudioRecorder();
      this.audioRecorder.onError = (err) => {
        console.error('Error:', err);
      }
      this.audioRecorder.onDataAvailable = (data) => {
        console.log('Data:', data);
        this.recordedAudioBlob = data;
      }
    },
    methods: {
      startRecording() {
        this.audioRecorder.start();
        this.recording = true;
        setTimeout(() => {
          this.stopRecording();
        }, 10000);
      },
      stopRecording() {
        this.audioRecorder.stop();
        this.recording = false;
      },
      playRecordedAudio() {
        const audioUrl = URL.createObjectURL(this.recordedAudioBlob);
        const audio = new Audio(audioUrl);
        audio.play();
      }
    }
  }
</script>

示例2 - 加入波浪效果

下面是一个加入波浪效果的示例。在这个示例中,我们使用了canvas来绘制波浪图,通过requestAnimationFrame方法来不断重绘波浪图。

<template>
  <div>
    <div>
      <button @mousedown.prevent="startRecording" @touchstart.prevent="startRecording">开始录音</button>
      <button @click="stopRecording">停止录音</button>
    </div>
    <div>
      <button @click="playRecordedAudio">播放录制的音频</button>
    </div>
    <div>
      <canvas ref="canvas" width="500" height="100"></canvas>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        audioRecorder: null,
        recording: false,
        recordedAudioBlob: null,
        animationId: null,
        canvasContext: null
      }
    },
    mounted() {
      this.audioRecorder = new AudioRecorder();
      this.audioRecorder.onError = (err) => {
        console.error('Error:', err);
      }
      this.audioRecorder.onDataAvailable = (data) => {
        console.log('Data:', data);
        this.recordedAudioBlob = data;
      }

      this.canvasContext = this.$refs.canvas.getContext('2d');
      this.drawWaveform();
    },
    methods: {
      startRecording() {
        this.audioRecorder.start();
        this.recording = true;
        this.animationId = requestAnimationFrame(this.drawWaveform);
      },
      stopRecording() {
        this.audioRecorder.stop();
        this.recording = false;
        cancelAnimationFrame(this.animationId);
      },
      playRecordedAudio() {
        const audioUrl = URL.createObjectURL(this.recordedAudioBlob);
        const audio = new Audio(audioUrl);
        audio.play();
      },
      drawWaveform() {
        this.canvasContext.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
        if (this.recording) {
          const analyser = this.audioRecorder.getAnalyser();
          const bufferLength = analyser.fftSize;
          const dataArray = new Float32Array(bufferLength);
          analyser.getFloatTimeDomainData(dataArray);
          let maxValue = 0;
          for (let i = 0; i < dataArray.length; i++) {
            const value = Math.abs(dataArray[i]);
            if (value > maxValue) {
              maxValue = value;
            }
          }
          const waveformHeight = this.$refs.canvas.height * 0.6;
          const coef = waveformHeight / maxValue;
          this.canvasContext.beginPath();
          this.canvasContext.lineWidth = 3;
          this.canvasContext.strokeStyle = 'blue';

          const sliceWidth = this.$refs.canvas.width * 1.0 / bufferLength;
          let x = 0;
          for (let i = 0; i < bufferLength; i++) {
            const value = dataArray[i];
            const y = (value * coef) + waveformHeight / 2;
            if (i === 0) {
              this.canvasContext.moveTo(x, y);
            } else {
              this.canvasContext.lineTo(x, y);
            }
            x += sliceWidth;
          }
          this.canvasContext.stroke();
          this.animationId = requestAnimationFrame(this.drawWaveform);
        } else {
          this.canvasContext.beginPath();
          this.canvasContext.lineWidth = 3;
          this.canvasContext.strokeStyle = 'black';
          this.canvasContext.moveTo(0, this.$refs.canvas.height / 2);
          this.canvasContext.lineTo(this.$refs.canvas.width, this.$refs.canvas.height / 2);
          this.canvasContext.stroke();
        }
      }
    }
}
</script>

<style scoped>
  canvas {
    border: 1px solid black;
  }
</style>

以上两个示例可以较好地解释如何使用js-audio-recorder来在Vue中实现录音功能,同时加入波浪效果。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue实现录音功能js-audio-recorder带波浪图效果的示例 - Python技术站

(0)
上一篇 2023年6月20日
下一篇 2023年6月20日

相关文章

  • GTA5 PC版开车按键延迟怎么办 开车按键延迟解决方法介绍

    GTA5 PC版开车按键延迟怎么办 开车按键延迟解决方法介绍 在玩GTA5 PC版时,可能会遇到开车时按键反应延迟的问题,可能会影响到游戏体验。本攻略将介绍如何解决开车按键延迟的问题。 原因分析 造成开车按键延迟的原因主要有以下几个方面: 硬件原因:可能是您的电脑设备性能较低,或者您的输入设备(如鼠标、键盘、手柄等)存在问题。 软件原因:可能是游戏内存在卡顿…

    other 2023年6月27日
    00
  • JavaScript是如何实现继承的(六种方式)

    下面是 JavaScript 实现继承的六种方式的详细攻略: 1. 原型链继承 原型链继承是 JavaScript 实现继承最常用的方式之一。通过将子类的原型指向父类的实例,从而实现对父类属性和方法的继承。具体代码实现如下: // 父类 function Person(name) { this.name = name; } Person.prototype.…

    other 2023年6月26日
    00
  • 芯片封装技术全接触

    芯片封装技术全接触攻略 了解芯片封装技术的基础知识 在开始研究芯片封装技术之前,我们需要了解一些基础知识。首先,芯片是由半导体材料制成的微型电子元件,用于存储和处理信息。封装技术是将芯片封装成一个实际可用的器件,以保护芯片并方便它的使用。常见的芯片封装技术包括QFN、BGA、CSP等。 掌握芯片封装的设计原理 掌握芯片封装的设计原理对于进一步理解芯片封装技术…

    other 2023年6月25日
    00
  • Android学习之基础知识四-Activity活动8讲(活动的灵活运用)

    Android学习之基础知识四-Activity活动8讲(活动的灵活运用) Activity是Android应用程序的核心组件之一,它负责管理应用程序的用户界面和生命周期。本文将为您详细讲解Activity的基础知识和灵活运用方法,包括Activity的生命周期、启动模式、任务和栈等内容。 Activity的生命周期 Activity的生命周期是指Activ…

    other 2023年5月6日
    00
  • PHP ElasticSearch做搜索实例讲解

    下面就是“PHP ElasticSearch做搜索实例讲解”的完整攻略: 什么是 ElasticSearch? ElasticSearch 是一个开源分布式搜索引擎,使用 Java 编写,底层基于 Lucene 实现。它具有高度的伸缩性、全文搜索功能以及近乎实时的搜索速度,采用 JSON 作为数据交换格式,可以处理结构化、半结构化和非结构化的数据。 Elas…

    other 2023年6月27日
    00
  • 老生常谈Java中instanceof关键字的理解

    老生常谈 Java 中 instanceof 关键字的理解 1. 概述 instanceof 是 Java 中一个关键字,其功能是判断一个对象是否为某个类的实例。它的格式如下: 对象 instanceof 类型 其中,对象 是要检查的对象,类型 是要检查的类型。如果 对象 是 类型 的实例,返回 true;否则返回 false。 2. 例子解析 2.1. 例…

    other 2023年6月26日
    00
  • bmob—移动后端云服务平台

    bmob—移动后端云服务平台 移动互联网已经成为当今社会的主流。越来越多的企业和个人开始开发移动应用,而一个好的应用需要有一个稳定可靠、高扩展性的后端服务支撑。这时,就需要一个好的云服务平台来满足这些需求。 bmob是国内领先的移动后端云服务平台,提供了从数据存储、推送服务到即时通讯服务、物联网平台等全方位的移动后端云服务支撑。bmob所提供的服务简单易用,…

    其他 2023年3月28日
    00
  • C#设计模式系列:备忘录模式(Memento)

    C#设计模式系列:备忘录模式(Memento) 备忘录模式是一种行为型设计模式,它允许你将对象的状态保存到备忘录中并在以后的时间点将对象恢复到先前的状态。 适用场景 需要即时保存和恢复内部状态的对象。 当直接访问对象内部状态的成本比通过备忘录进行恢复操作更高时。 需要保留对象状态历史记录的情况,以便回滚操作等。 实现方法 备忘录模式包含三个主要的角色:Ori…

    其他 2023年3月28日
    00
合作推广
合作推广
分享本页
返回顶部