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

发布时间:2024年01月19日

报错

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

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
离谱的错误…

文章来源:https://blog.csdn.net/qq_62835094/article/details/135692592
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。