记录一个很蠢的错误。点击采集录音
,出现报错:
audio.html:63 TypeError: Failed to execute 'getUserMedia' on 'MediaDevices': At least one of audio and video must be requested
at HTMLButtonElement.onOpenAudio (audio.html:66:32)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<audio id="audio" autoplay controls>录音</audio>
<button id="showAudio">采集录音</button>
<p>通过getUserMedia()方法获取摄像头数据</p>
</body>
<script>
const constraints = {
autdio: true,
video: false
}
// 处理打开摄像头成功
function handleSuccess(stream) {
const audio = document.querySelector("#audio");
audio.srcObject = stream
}
// 处理打开摄像头失败
function handleError(error) {
console.log(error);
}
function onOpenAudio(e) {
navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError);
}
document.querySelector("#showAudio").addEventListener("click", onOpenAudio)
</script>
</html>
const constraints = {
autdio: true,
video: false
}
audio写错写成了autdio,所以浏览器找不到audio
离谱的错误…