原生js实现摄像头麦克风视频录制和下载效果代码

代码语言:html

所属分类:多媒体

代码描述:原生js实现摄像头麦克风视频录制和下载效果代码

代码标签: 摄像头 麦克风 视频 声音 录制 下载

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">





    <style>
        .layout {
          background-color: #fff;
          margin: 50px;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
        }
        .layout__video .video__player {
          background-color: #000;
          width: 500px;
          height: 400px;
        }
        .layout__video .video__player.video__player--recorder {
          height: 368px;
          margin-top: -10px;
        }
        .layout__video .video__player.video__player--player {
          margin-left: 50px;
        }
        .layout__video .video__buttons {
          border: 0;
          background-color: #fafafa;
          font-weight: 100;
          font-size: 12px;
          margin-top: -4px;
          padding: 4px;
        }
        .layout__video .video__buttons .video__buttons--record {
          margin-top: 2px;
        }
        
        @media screen and (max-width: 1150px) {
          .layout {
            flex-direction: column;
          }
          .layout__video .video__player.video__player--player {
            margin-left: 0;
            margin-top: 50px;
          }
        }
    </style>



</head>

<body>
    <div class="layout">
        <div class="layout__video">
            <video class="video__player video__player--recorder" id="js-video-recorder" autoplay muted></video>
            <div class="video__buttons">
                <button class="video__buttons video__buttons--record" id="js-button-record" disabled>Start Recording</button>
                <button class="video__buttons video__buttons--play" id="js-button-play" disabled>Play</button>
                <button class="video__buttons video__buttons--download" id="js-button-download" disabled>Download</button>
            </div>
        </div>
        <div class="layout__video">
            <video class="video__player video__player--player" id="js-video-player" loop controls></video>
        </div>
    </div>



    <script>
        /*
        *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
        *
        *  Use of this source code is governed by a BSD-style license
        *  that can be found in the LICENSE file in the root of the source
        *  tree.
        */
        
        // This code is adapted from
        // https://rawgit.com/Miguelao/demos/master/mediarecorder.html
        
        'use strict';
        
        /* globals MediaRecorder */
        
        var mediaSource = new MediaSource();
        mediaSource.addEventListener('sourceopen', handleSourceOpen, false);
        var mediaRecorder;
        var recordedBlobs;
        var sourceBuffer;
        
        var gumVideo = document.querySelector('video#js-video-recorder');
        var recordedVideo = document.querySelector('video#js-video-player');
        
        var recordButton = document.querySelector('button#js-button-record');
        var playButton = document.querySelector('button#js-button-play');
        var downloadButton = document.querySelector('button#js-button-download');
        recordButton.onclick = toggleRecording;
        playButton.onclick = play;
        downloadButton.onclick = download;
        
        // window.isSecureContext could be used for Chrome
        var isSecureOrigin = location.protocol === 'https:' ||
        location.hostname === 'localhost';
        if (!isSecureOrigin) {
          alert('getUserMedia() must be run from a secure origin: HTTPS or localhost.' +
          '\n\nChanging protocol to HTTPS');
          location.protocol = 'HTTPS';
        }
        
        var constraints = {
          audio: true,
          video: true };
        
        
        function handleSuccess(stream) {
          recordButton.disabled = false;
          console.log('getUserMedia() got stream: ', stream);
          window.stream = stream;
          gumVideo.srcObject = stream;
        }
        
        function handleError(error) {
          console.log('navigator.getUserMedia error: ', error);
          if (error.name === "NotAllowedError") {
            alert(`You need to allow access to your camera and microphone for this page.`);
          }.........完整代码请登录后点击上方下载按钮下载查看

网友评论0