当前位置:首页 > VUE

vue中实现录音下载

2026-01-22 04:53:04VUE

Vue 中实现录音下载的方法

使用 Web Audio API 和 MediaRecorder

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

npm install recordrtc

在 Vue 组件中引入相关库:

import RecordRTC from 'recordrtc';

创建录音逻辑:

export default {
  data() {
    return {
      mediaRecorder: null,
      audioChunks: [],
      audioBlob: null,
      audioUrl: ''
    };
  },
  methods: {
    async startRecording() {
      const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
      this.mediaRecorder = new MediaRecorder(stream);

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

      this.mediaRecorder.onstop = () => {
        this.audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' });
        this.audioUrl = URL.createObjectURL(this.audioBlob);
        this.audioChunks = [];
      };

      this.mediaRecorder.start();
    },

    stopRecording() {
      this.mediaRecorder.stop();
      this.mediaRecorder.stream.getTracks().forEach(track => track.stop());
    },

    downloadRecording() {
      const a = document.createElement('a');
      a.href = this.audioUrl;
      a.download = 'recording.wav';
      document.body.appendChild(a);
      a.click();
      setTimeout(() => {
        document.body.removeChild(a);
        URL.revokeObjectURL(this.audioUrl);
      }, 100);
    }
  }
};

使用第三方库简化实现

使用 vue-audio-recorder 插件:

npm install vue-audio-recorder

在组件中使用:

import VueAudioRecorder from 'vue-audio-recorder';

export default {
  components: {
    VueAudioRecorder
  },
  methods: {
    onRecordingComplete(audio) {
      const url = URL.createObjectURL(audio.blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = 'recording.mp3';
      a.click();
    }
  }
};

注意事项

  • 需要 HTTPS 环境或 localhost 才能使用 getUserMedia API
  • 不同浏览器支持的音频格式可能不同
  • 长时间录音需要考虑内存管理
  • 移动端可能需要额外权限处理

样式和模板示例

<template>
  <div>
    <button @click="startRecording">开始录音</button>
    <button @click="stopRecording">停止录音</button>
    <button @click="downloadRecording" :disabled="!audioUrl">下载录音</button>
  </div>
</template>

兼容性处理

对于不支持 MediaRecorder 的浏览器,可以使用 RecordRTC 作为 fallback:

const recorder = new RecordRTC(stream, {
  type: 'audio',
  mimeType: 'audio/wav'
});

vue中实现录音下载

标签: vue
分享给朋友:

相关文章

vue实现图片搜索

vue实现图片搜索

Vue实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue实现图片分析

vue实现图片分析

Vue 实现图片分析的方法 在 Vue 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas 进行…

vue表格重置怎么实现

vue表格重置怎么实现

重置表格数据的方法 在Vue中重置表格数据通常需要操作绑定的数据源,以下是几种常见实现方式: 直接重置数据源 // 假设表格绑定的是tableData this.tableData = [...t…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…