录制用户的音频,视屏 navigator.mediaDevices.getUserMedia

google 文档 HACKS 文档 相关代码

获取本地的音频

  <input type="file" accept="audio/*" capture="microphone" id="recorder">
  <audio id="player" controls></audio>
  <script>
    var recorder = document.getElementById('recorder');
    var player = document.getElementById('player')

    recorder.addEventListener('change', function (e) {
      var file = e.target.files[0];
      // Do something with the audio file.
      player.src = URL.createObjectURL(file);
    });
  </script>

获取麦克风声音

声音极小

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        a,
        button {
            border: 0;
            background-color: #448aff21;
            text-decoration: none;
            padding: 10px;
            border-radius: 2px;
            color: #448aff !important;
        }
    </style>
</head>

<body style="margin-top: 20px;">
    <a id="download">Download</a>
    <button id="start">Start</button>
    <button id="stop">Stop</button>
    <script>
        let l = console.log
        navigator.permissions.query({
            name: 'microphone'
        }).then(function (result) {
            if (result.state == 'granted') { // 用户之前已授予对麦克风的访问权
                l('ok')
            } else if (result.state == 'prompt') { // 用户尚未授予访问权,调用 getUserMedia 时将会收到提示
                l('ready')
            } else if (result.state == 'denied') { // 系统或用户已显式屏蔽对麦克风的访问权,您将无法获得对其的访问权
                l('...')
            }
            result.onchange = function () {
                l('change..')
            };
        });
        const downloadLink = document.getElementById('download');
        const stopButton = document.getElementById('stop');
        const startButton = document.getElementById('start');

        navigator.mediaDevices.getUserMedia({
                audio: true,
                video: false
            })
            .then(stream => {
                stopButton.addEventListener('click', e => {
                    mediaRecorder.stop();
                })

                startButton.addEventListener('click', e => {
                    mediaRecorder.start();
                })
                const recordedChunks = [];
                const mediaRecorder = new MediaRecorder(stream);

                mediaRecorder.addEventListener('dataavailable', function (e) {
                    if (e.data.size > 0) {
                        recordedChunks.push(e.data);
                    }
                });

                mediaRecorder.addEventListener('stop', function () {
                    l('暂停')
                    downloadLink.href = URL.createObjectURL(new Blob(recordedChunks));
                    downloadLink.download = 'acetest.wav';
                });

                mediaRecorder.addEventListener('start', e => {
                    l('开始')
                })
            });
    </script>
</body>

镶嵌在 audio元素里面

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        a,
        button {
            border: 0;
            background-color: #448aff21;
            text-decoration: none;
            padding: 10px;
            border-radius: 2px;
            color: #448aff !important;
        }
    </style>
</head>

<body style="margin-top: 20px;">
    <button id="start">Start</button>
    <button id="stop">Stop</button>
    <div>
        <audio id="audio" controls></audio>
    </div>
    <script>
        let l = console.log
        const stopButton = document.getElementById('stop');
        const startButton = document.getElementById('start');

        navigator.mediaDevices.getUserMedia({
                audio: true,
                // video: true
            })
            .then(stream => {
                stopButton.addEventListener('click', e => {
                    mediaRecorder.stop();
                })

                startButton.addEventListener('click', e => {
                    mediaRecorder.start();
                })
                const recordedChunks = [];
                const mediaRecorder = new MediaRecorder(stream);
                mediaRecorder.addEventListener('dataavailable', function (e) {
                    if (e.data.size > 0) {
                        recordedChunks.push(e.data);
                    }
                });

                mediaRecorder.addEventListener('stop', function () {
                    l('暂停')
                    var audio = document.getElementById('audio');
                    audio.src = URL.createObjectURL(new Blob(recordedChunks));
                    audio.play();
                });

                mediaRecorder.addEventListener('start', e => {
                    l('开始')
                })
            });
    </script>
</body>

录制视频 github地址

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      a,
      button {
        border: 0;
        background-color: #448aff21;
        text-decoration: none;
        padding: 10px;
        border-radius: 2px;
        color: #448aff !important;
      }
    </style>
  </head>

  <body style="margin-top: 20px;">
    <button id="start">Start</button> <button id="stop">Stop</button>
    <div>
      <p>live. <span class="timer"></span></p>
      <video
        width="600"
        id="live"
        style="box-sizing: border-box; border: 4px solid #f48"
      ></video>
    </div>
    <script>
      let l = console.log;
      let n = 0;

      let mediaRecorder;
      let timer;
      const stopButton = document.getElementById("stop");
      const startButton = document.getElementById("start");

      navigator.mediaDevices
        .getUserMedia({
          audio: true,
          video: true,
        })
        .then(stream => {
          let liveVideo = document.getElementById("live");
          // liveVideo.src = URL.createObjectURL(stream); // 你会看到一些警告
          liveVideo.srcObject = stream;
          liveVideo.play();

          stopButton.addEventListener("click", stopLive);
          startButton.addEventListener("click", e => {
            startLive(stream);
          });
        });

      // 显示录制的秒数
      function logger() {
        const timerEl = document.querySelector(".timer");
        timer = setInterval(() => {
          n += 1;
          timerEl.textContent = `${n}s`;
        }, 1000);
      }

      // 暂停后下载视频
      function downLoadVideo(chunks) {
        let downloadLink = document.createElement("a");
        downloadLink.href = URL.createObjectURL(
          new Blob(chunks, {
            type: "application/video",
          })
        );
        // downloadLink.download = 'live.webm';
        downloadLink.download = "live.ogg";
        // downloadLink.download = 'live.mp4';
        downloadLink.click();
      }

      // 结束录制
      function stopLive() {
        clearInterval(timer);
        n = 0;
        if (mediaRecorder) {
          mediaRecorder.stop();
        } else {
          alert("还没有开始。");
        }
      }

      function startLive(stream) {
        logger();
        let recordedChunks = [];
        mediaRecorder = new MediaRecorder(stream);
        mediaRecorder.start();

        mediaRecorder.addEventListener("dataavailable", function(e) {
          if (e.data.size > 0) recordedChunks.push(e.data);
        });

        mediaRecorder.addEventListener("stop", function() {
          l("暂停 自动下载");
          downLoadVideo(recordedChunks);
        });

        mediaRecorder.addEventListener("start", e => {
          l("开始 录制");
        });
      }
    </script>
  </body>
</html>

转载于:https://www.cnblogs.com/ajanuw/p/8422350.html

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值