当前位置: 首页 > news >正文

web及h5录音wav下载

效果图

在这里插入图片描述
注意必须要 https环境

代码如下:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Cache" content="no-cache">
<meta name="viewport"content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no,viewport-fit=cover"><head><title>WAV音频录制</title>
<script src="https://raw.githubusercontent.com/mattdiamond/Recorderjs/master/dist/recorder.js"></script>
</head><body><h1>WAV音频录制</h1><button id="startRecord">开始录制</button><button id="stopRecord" disabled>停止录制</button><button id="downloadRecord" disabled>下载WAV</button><audio id="audioPlayback" controls></audio><div id="status"></div><script>document.addEventListener('DOMContentLoaded', function () {const startButton = document.getElementById('startRecord');const stopButton = document.getElementById('stopRecord');const downloadButton = document.getElementById('downloadRecord');const audioPlayback = document.getElementById('audioPlayback');const statusDiv = document.getElementById('status');let audioContext;let recorder;let audioStream;let audioBlob;// 兼容性处理:确保 mediaDevices 存在if (!navigator.mediaDevices) {navigator.mediaDevices = {};}if (!navigator.mediaDevices.getUserMedia) {navigator.mediaDevices.getUserMedia = function (constraints) {const legacyGetUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;if (!legacyGetUserMedia) {return Promise.reject(new Error('getUserMedia is not supported!'));}return new Promise((resolve, reject) => {legacyGetUserMedia.call(navigator, constraints, resolve, reject);});};}// 开始录制startButton.addEventListener('click', async function () {statusDiv.textContent = "正在请求麦克风权限...";try {audioStream = await navigator.mediaDevices.getUserMedia({ audio: true });audioContext = new (window.AudioContext || window.webkitAudioContext)({latencyHint: 'interactive' // iOS 兼容});const source = audioContext.createMediaStreamSource(audioStream);recorder = new Recorder(source);recorder.record();startButton.disabled = true;stopButton.disabled = false;statusDiv.textContent = "正在录制...";} catch (err) {statusDiv.textContent = "错误: " + err.message;console.error(err);}});// 停止录制stopButton.addEventListener('click', function () {if (recorder) {recorder.stop();startButton.disabled = false;stopButton.disabled = true;statusDiv.textContent = "正在处理音频...";recorder.exportWAV(function (blob) {audioBlob = blob;const audioUrl = URL.createObjectURL(blob);audioPlayback.src = audioUrl;downloadButton.disabled = false;statusDiv.textContent = "录制完成!";audioStream.getTracks().forEach(track => track.stop());});}});// 下载录音downloadButton.addEventListener('click', function () {if (!audioBlob) {alert('请先完成录音!');return;}// 创建Blob URL(明确指定MIME类型)const url = URL.createObjectURL(new Blob([audioBlob], { type: 'audio/wav' }));// 创建隐藏的<a>标签const a = document.createElement('a');a.href = url;a.download = 'recording_' + Date.now() + '.wav'; // 避免文件名冲突a.style.display = 'none';document.body.appendChild(a);// 直接触发点击(不再使用MouseEvent模拟)a.click();// 延迟清理(确保下载开始后再移除)setTimeout(() => {document.body.removeChild(a);URL.revokeObjectURL(url);}, 1000); // 延长延迟时间确保下载});});</script>
</body></html>
http://www.dtcms.com/a/520717.html

相关文章:

  • Kotlin 协程中常见的异步返回与控制方式(速览)
  • 做网站还有前景么动漫网页设计报告
  • Maven 多配置文件的使用
  • 【双机位A卷】华为OD笔试之【哈希表】双机位A-跳房子I【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
  • SQL 拼接完全指南
  • 制作的网站wordpress还是自己写
  • 【HLS】Java实现统计HLS的m3u8清单中所有ts切片的视频持续时长
  • 免费网站建设ppt模板下载山西省建设银行网站首页
  • 增城网站建设价格郑州seo
  • 【Rust实战】从零构建高性能异步Web服务器:深入理解所有权与生命周期
  • Vlan-ACCESS接口+Trunk接口
  • 网站开发遇到的最大困难被k掉的网站怎么做才能有收录
  • SpringBoot-Web开发之文件上传
  • 5.2 类
  • 厦门协会网站建设电影网站做淘客
  • 网站建设介绍书如何注销公司流程及费用
  • 阿里国际站网站建设wordpress mysql 扩展
  • LeetCode 405 - 数字转换为十六进制数
  • 漳州做网站喊多少钱wordpress栏目更改无法显示
  • 集团公司网站欣赏如何做企业网站内链
  • 未来的 AI 操作系统(九)——灵魂架构:当智能系统拥有“自我”
  • 卡码网语言基础课(Python) | 20.排队取奶茶
  • ManySpeech —— 使用 C# 开发人工智能语音应用
  • 5G-A 与 5G 对比
  • 网站建设与 宣传关系wordpress 订单
  • Linux进程信号(贰):保存信号
  • 互联网站建设 天津台州网站建设惠店
  • 基于Python大数据的主流汽车价格分析可视化系统
  • Flutter状态管理原理详解
  • 如何选择网站项目企业营销推广怎么做