1 Star 0 Fork 0

cinarsky/cinarsky.github.io

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
index.html 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
cinarsky 提交于 2024-11-13 07:26 . test
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>录音示例</title>
</head>
<body>
<h1>录音示例</h1>
<button id="startBtn">开始录音</button>
<button id="stopBtn" disabled="">停止录音</button>
<audio id="audioPlayer" controls=""></audio>
<script>
let mediaRecorder;
let audioChunks = [];
document.getElementById("startBtn").onclick = async function () {
alert(navigator.mediaDevices)
// 请求用户权限使用麦克风
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
// alert(1)
document.getElementById("stopBtn").disabled = false;
document.getElementById("startBtn").disabled = true;
mediaRecorder.ondataavailable = function (event) {
alert(1);
audioChunks.push(event.data);
};
mediaRecorder.onstop = function () {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
const audioUrl = URL.createObjectURL(audioBlob);
const audioPlayer = document.getElementById("audioPlayer");
audioPlayer.src = audioUrl;
audioChunks = [];
};
};
document.getElementById("stopBtn").onclick = function () {
mediaRecorder.stop();
document.getElementById("stopBtn").disabled = true;
document.getElementById("startBtn").disabled = false;
};
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cinarsky/cinarsky.github.io.git
git@gitee.com:cinarsky/cinarsky.github.io.git
cinarsky
cinarsky.github.io
cinarsky.github.io
master

搜索帮助