TypeError: Failed to execute ‘getUserMedia‘ on ‘MediaDevices‘

报错

记录一个很蠢的错误。点击采集录音,出现报错:

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)

TypeError: Failed to execute ‘getUserMedia‘ on ‘MediaDevices‘_第1张图片

源代码

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
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
离谱的错误…

你可能感兴趣的:(javascript,前端,开发语言)