vue录音+js-audio-recorder

article/2025/10/15 6:40:28

小小记录一下项目中用到的录音功能

1.下载插件

npm i js-audio-recorder

2.展示代码

<template><div style="padding: 20px;"><div style="font-size:14px"><h3>录音时长:{{ recorder && recorder.duration.toFixed(0) }}s</h3><br /><button type="primary" @click="handleStart">开始录音</button><button type="warning" @click="handleStop">停止录音</button><br /><br /><h3>播放时长:{{recorder &&(playTime > recorder.duration? recorder.duration.toFixed(0): playTime.toFixed(0))}}s</h3><br /><button type="primary" @click="handlePlay">播放录音</button><button type="info" @click="handlePausePlay">暂停播放</button><button type="success" @click="handleResumePlay">继续播放</button><button type="warning" @click="handleStopPlay">停止播放</button><button type="primary" @click="uploadRecord">上传</button></div></div>
</template><script>
import Recorder from 'js-audio-recorder'
const parameter = {sampleBits: 8, // 采样位数,支持 8 或 16,默认是16sampleRate: 11025, // 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000numChannels: 1 // 声道,支持 1 或 2, 默认是1
}
export default {data() {return {recorder: null,playTime: 0,timer: null,src: null,setTime:0}},created() {this.recorder = new Recorder(parameter)},methods: {// 开始录音handleStart() {var that = thisthat.recorder = new Recorder()Recorder.getPermission().then(() => {console.log('开始录音')that.recorder.start()// 开始录音
//限制录音时长that.setTime= setTimeout(function(){that.handleStop()alert('录音时间到')},180000)}, (error) => {that.$message({message: '请先允许该网页使用麦克风',type: 'info'})console.log(`${error.name} : ${error.message}`)})},handleStop() {console.log('停止录音')this.recorder.stop() // 停止录音clearTimeout(this.setTime)// this.recorder.duration= 0;console.log('时长',this.recorder.duration)},handlePlay() {console.log('播放录音')console.log(this.recorder)this.recorder.play() // 播放录音// 播放时长this.timer = setInterval(() => {try {this.playTime = this.recorder.getPlayTime()} catch (error) {this.timer = null}}, 100)},handlePausePlay() {console.log('暂停播放')this.recorder.pausePlay() // 暂停播放// 播放时长this.playTime = this.recorder.getPlayTime()this.time = null},handleResumePlay() {console.log('恢复播放')this.recorder.resumePlay() // 恢复播放// 播放时长this.timer = setInterval(() => {try {this.playTime = this.recorder.getPlayTime()} catch (error) {this.timer = null}}, 100)},handleStopPlay() {console.log('停止播放')this.recorder.stopPlay() // 停止播放// 播放时长this.playTime = this.recorder.getPlayTime()this.timer = null},handleDestroy() {console.log('销毁实例')this.recorder.destroy() // 毁实例this.timer = null},uploadRecord() {if (this.recorder == null || this.recorder.duration === 0) {console.log('请先录音')// this.$message({//   message: '请先录音',//   type: 'error'// })return false}this.recorder.pause() // 暂停录音this.timer = nullconsole.log('上传录音')// 上传录音const formData = new FormData()const blob = this.recorder.getWAVBlob()// 获取wav格式音频数据// 此处获取到blob对象后需要设置fileName满足当前项目上传需求,其它项目可直接传把blob作为file塞入formDataconst newbolb = new Blob([blob], { type: 'audio/wav' })const fileOfBlob = new File([newbolb], new Date().getTime() + '.wav')formData.append('file', fileOfBlob)const url = window.URL.createObjectURL(fileOfBlob)//通过blob对象获取到播放urlthis.src = urlconsole.log(blob)// const axios = require('axios')上传后台// axios.post(url, formData).then(res => {//   console.log(res.data.data[0].url)// })}}
}
</script>

重点:最后发现停止录音还会占用录音权限

稍作修改(停止录音把录音文件传入blob,关闭录音用到销毁实例会删除这个流让录音的小标志消失)

1.

    // 开始录音handleStart() {var that = this;that.recorder = new Recorder();Recorder.getPermission().then(() => {that.kFlag = false;that.dFlag = true;console.log("开始录音");that.recorder.start(); // 开始录音clearTimeout(this.setTime);that.setTime = setTimeout(function () {that.stopRecord();alert("录音时间到");}, 180000);},(error) => {that.$message({message: "请先允许该网页使用麦克风",type: "info",});console.log(`${error.name} : ${error.message}`);});},

2.

    stopRecord() {if (this.recorder == null || this.recorder.duration === 0) {Dialog({message: `<div style="font-size:18px;">请先录音</div>`,width: 200,height: 50,});return false;}console.log("停止录音");this.kFlag = true;this.dFlag = false;this.recorder.stop(); // 停止录音// this.drawRecordId && cancelAnimationFrame(this.drawRecordId);//   this.drawRecordId = null;clearTimeout(this.setTime);const formData = new FormData();this.blob = this.recorder.getWAVBlob(); // 获取wav格式音频数据// 此处获取到blob对象后需要设置fileName满足当前项目上传需求,其它项目可直接传把blob作为file塞入formDataconst newbolb = new Blob([this.blob], { type: "audio/wav" });const fileOfBlob = new File([newbolb], new Date().getTime() + ".wav");formData.append("file", fileOfBlob);const url = window.URL.createObjectURL(fileOfBlob); //通过blob对象获取到播放urlthis.src = url;console.log(this.blob);},

3.

    handleStop() {console.log("关闭录音");this.recorder.destroy(); // 毁实例console.log(this.blob);},

成果 

 

 

 

 

 

 需要其他效果可以通过

 3.插件地址Introduction · recorder

自行添加


http://chatgpt.dhexx.cn/article/55oBAmVj.shtml

相关文章

html 苹果微信录音js,微信js-sdk 录音功能的示例代码

需求描述 制作一个H5页面&#xff0c;打开之后可以录音&#xff0c;并将录音文件提交至后台 微信录音最长时长为1min 代码如下 // isVoice&#xff1a; 0-未录音 1-录音中 2-录完音 // 点击录音/录音中 按钮展示 点击录音 // isListen // 0-未试听/试听结束 1-试听中 2-暂停试听…

已解决:H5移动端网页实现录音功能,js实现录音功能,包括安卓webview接口也可以使用

遇到一个需求&#xff0c;需要做一个手机网页录音的功能&#xff0c;嵌入到webview中去&#xff0c;用安卓原生录音倒是可以&#xff0c;但是想着尽量去安卓化开发&#xff0c;就想着用纯的js前端代码去实现录音功能。 在 Web 应用程序中&#xff0c;JavaScript 是运行在浏览器…

网页录音时的麦克风权限问题解决

来源 | https://www.html.cn/web/html/19184.html 在我们进行网页制作时可能会遇到需要录音的情况&#xff0c;而在进行网页录音时又有可能会遇到麦克风权限问题导致无法录音&#xff0c;本文就来为大家介绍一下如何解决麦克风权限问题。 在本地中打开的时候&#xff0c;谷歌、…

获取摄像头和麦克风权限_js获取浏览器摄像头和麦克风权限

前言 项目中会使用到摄像头或麦克风设备&#xff0c;这就需要我们获取浏览器的摄像头和麦克风权限&#xff0c;权限是无法通过js操控的&#xff0c;必须由浏览器用户设置。 下面我来告诉大家如何获取浏览器的摄像头或麦克风的权限&#xff0c;使浏览器弹出询问窗口。 一、我们想…

(前端)录音功能实现 js-audio-recorder

1. 创建 import Recorder from js-audio-recorder const parameter {sampleBits: 16, // 采样位数&#xff0c;支持 8 或 16&#xff0c;默认是16sampleRate: 8000, // 采样率&#xff0c;支持 11025、16000、22050、24000、44100、48000&#xff0c;根据浏览器默认值&#x…

Android webview录音权限和音频自动播放

项目背景&#xff1a;我们通过layabox&#xff0c;制作了H5页面&#xff0c;可以在微信中&#xff0c;手机浏览器中使用&#xff0c;现在需要将H5页面集成到Android的APP中。 遇到的问题&#xff1a; 1.遇到的第一个问题&#xff0c;是获取录音权限的问题&#xff0c;我已经给…

html 苹果微信录音js,基于JS开发微信网页录音功能的实例代码

具体代码如下所示&#xff1a; wx.ready(function () { var startRecordflag false var startTime null //btnRecord 为录音按钮dom对象 btnRecord.addEventListener(touchstart, function (event) { event.preventDefault(); startTime newDate().getTime(); // 延时后录音…

std::string 与 std::wstring 互转

前言: 最近接触了一些 win32 方便的编程,由于不熟 可能会写一写这方便的基础东西 相当于 写日记了 提升一下 他们的声明 string 是 char wstring 是wchar_t 什么是wchar_t ? string 转 wstring inline std::wstring StringToWString(const std::string& str) {int len…

C++ 字符串string、字符char、宽字符数组wstring、宽字符wchar_t互相转换(2021.4.20)

C单字符串和宽字符串学习 2021.4.20 1、char 和 string1.1 单字符 char1.2 单字符数组 char[] 和 char*1.2.1 char[]1.2.2 char* 1.3 单字符串 string1.4 char[] 转 string1.5 cha[] 转char*1.6 string 转 char*1.7 string 转 char[] 2、wchar_t 和 wstring2.1 宽字符 wchar_t2…

string和wstring相互转换以及wstring显示中文问题

如果你只是使用C来处理字符串&#xff0c;会用到string。不过string是窄字符串ASCII&#xff0c;而很多Windows API函数用的是宽字符Unicode。这样让string难以应对。作为中国的程序员&#xff0c;我们第一个想到的字符串就是中文&#xff0c;而不是英文。所以经常会遇到中文字…

C++里面,什么时候使用std::wstring

看你要使用什么字符编码了&#xff0c; std::wstring主要用于 UTF-16编码的字符,而std::string主要用于存储单字节的字符( ASCII字符集 )&#xff0c;但是也可以用来保存UTF-8编码的字符。&#xff08;UTF-8和UTF-16是UNICODE字符集的两种不同的字符编码&#xff09; 如果你的…

String类与wstring类的区别

String类与wstring类的区别 本质区别 存储字符的区别 #include <iostream> #include <string> using namespace std; int main() { wstring wstr1 L"你好世界"; // L普通字符串 宽字符串 const wchar_t *ch1 wstr1.c_str(); // 转化为宽字符…

从新建工程开始使用C++开发单片机(以STM32为例):七、移植Arduino的WString(附代码)

在上一篇文章中&#xff0c;介绍了outputString和inputString&#xff0c;其中所运用到的字符串类型String也是C驱动层中功能强大且重要的一个类。这个类移植自Arduino的WString。这篇文章将会展示WString的易用性&#xff0c;并且从编译输出后程序大小的角度比较WSting和C std…

0005:Qt常用类 - QDateTime

Qt常用类 - QDateTime 1 开发环境 在介绍内容之前&#xff0c;先说明一下开发环境&#xff0c;如下图&#xff1a; Qt版本&#xff1a;Qt5.3.2&#xff1b; Qt开发工具&#xff1a;Qt Creater 3.2.1&#xff1b; Qt构建工具&#xff1a;Desktop Qt 5.3 MinGW 32bit&#xff…

QDateTime的11种显示方式

QDateTime datetime QDateTime::currentDateTime(); datetime.toString(“hh:mm:ss\nyyyy/MM/dd”); datetime.toString(“hh:mm:ss ap\nyyyy/MM/dd”); datetime.toString(“hh:mm:ss\nyyyy-MM-dd”); datetime.toString(“hh:mm:ss ap\nyyyy-MM-dd”); datetime.to…

70 QDateTime时间戳转换有误

1 前言 在开发工具中需要用时间戳转换成格式化时间来显示&#xff0c;但引用QT中自带的时间类QDateTime转换时&#xff0c;发现转换时间有误问题&#xff0c;转换的结果时分秒是正确的&#xff0c;但月份确实错误的。因此在未深入研究qt实现情况下&#xff0c;需要得到正确的格…

QDateTime类的部分函数使用解析

QDateTime类提供了日期和时间功能。 QDateTime对象包含一个日历日期和一个时钟时间&#xff08;一个“日期时间”&#xff09;。它是QDate和QTime两个类的组合。它可以从系统时钟中读取当前日期时间。它提供比较日期时间和操作日期时间的函数&#xff0c;比如加上一定数量的秒…

QDateTime时间相减

qDebug()<<"span"<<QDateTime::fromMSecsSinceEpoch(QDateTime::currentDateTime().toMSecsSinceEpoch() - time.toMSecsSinceEpoch()).toUTC().toString("hh:mm:ss"); 1、使用 QDateTime::toMSecsSinceEpoch 计算时间差&#xff1b; 2、使用…

小心 QDateTime

QDateTime 时间跳变问题接收服务端时间问题 时间跳变问题 QDateTime的具体用法可以在Qt creator 鼠标双击选中&#xff0c;按 F1 查看它的文档, Qt 的文档还是不错的. 我们有时会用下面的代码来做等待, 在笔者使用过程中发现 //等待QDateTime n QDateTime::currentDateTime(…

QDateTime

一、描述 QDateTime 对象对日历日期和时钟时间进行编码。它结合了 QDate 和 QTime 类的特性。它可以从系统时钟中读取当前日期时间。它提供了用于比较日期时间和通过添加秒数、天数、月数或年数来操作日期时间的函数。 二、成员函数 1、QString toString(const QString &…