当前位置:首页 > VUE

vue实现语音录制

2026-01-18 04:10:28VUE

Vue 实现语音录制的方法

使用 Web Audio API 和 MediaRecorder

安装必要的依赖(如需要):

npm install recordrtc

在 Vue 组件中引入相关库:

import RecordRTC from 'recordrtc';

创建录音功能的核心代码:

export default {
  data() {
    return {
      recorder: null,
      audioChunks: [],
      audioBlob: null,
      audioUrl: null,
      isRecording: false
    };
  },
  methods: {
    async startRecording() {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
        this.recorder = new RecordRTC(stream, {
          type: 'audio',
          mimeType: 'audio/wav'
        });
        this.recorder.startRecording();
        this.isRecording = true;
      } catch (error) {
        console.error('Error accessing microphone:', error);
      }
    },
    stopRecording() {
      if (!this.recorder) return;
      this.recorder.stopRecording(() => {
        this.audioBlob = this.recorder.getBlob();
        this.audioUrl = URL.createObjectURL(this.audioBlob);
        this.isRecording = false;
        this.recorder.destroy();
        this.recorder = null;
      });
    }
  }
};

使用 vue-voice-recorder 插件

安装插件:

npm install vue-voice-recorder

在 Vue 项目中注册组件:

import VueVoiceRecorder from 'vue-voice-recorder';
Vue.use(VueVoiceRecorder);

在模板中使用:

<template>
  <div>
    <voice-recorder
      @start="onStart"
      @stop="onStop"
      @error="onError"
      @stream="onStream"
    />
    <audio v-if="audioUrl" :src="audioUrl" controls></audio>
  </div>
</template>

使用浏览器原生 API

纯浏览器 API 实现方案:

export default {
  methods: {
    async startNativeRecording() {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
        this.mediaRecorder = new MediaRecorder(stream);
        this.audioChunks = [];

        this.mediaRecorder.ondataavailable = event => {
          this.audioChunks.push(event.data);
        };

        this.mediaRecorder.onstop = () => {
          this.audioBlob = new Blob(this.audioChunks);
          this.audioUrl = URL.createObjectURL(this.audioBlob);
        };

        this.mediaRecorder.start();
      } catch (err) {
        console.error('Error:', err);
      }
    },
    stopNativeRecording() {
      if (this.mediaRecorder) {
        this.mediaRecorder.stop();
        this.mediaRecorder.stream.getTracks().forEach(track => track.stop());
      }
    }
  }
};

注意事项

确保在 HTTPS 环境下测试,某些浏览器在非安全环境下会限制录音功能

iOS 设备有特殊限制,需要用户主动交互才能启动录音

录音前需要获取用户授权,处理可能的权限拒绝情况

不同浏览器支持的音频格式可能不同,建议测试目标浏览器的兼容性

vue实现语音录制

录音文件大小可能较大,上传前考虑压缩或分段处理

标签: 语音vue
分享给朋友:

相关文章

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面:…