原生js实现浏览器端录制麦克风声音录音播放并下载效果代码

代码语言:html

所属分类:多媒体

代码描述:原生js实现浏览器端录制麦克风声音录音播放并下载效果代码

代码标签: 麦克风 录音 播放 下载

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

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width">



    <style>
        @import 'https://fonts.googleapis.com/icon?family=Material+Icons|Roboto';
        
        body {
          background: #fff;
          font-family: 'Roboto Light';
        }
        
        body,
        html {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        
        * {
            outline: none;
        }
        
        .recorder_wrapper {
            width: 100%;
            display: -webkit-flex;
            display: -moz-flex;
            display: -ms-flex;
            display: -o-flex;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 100%;
        }
        
        .recorder {
            display: inline-block;
            text-align: center;
            width: 500px;
            max-width: 100%;
        }
        
        .record_btn {
            width: 100px;
            height: 100px;
            font-family: 'Material Icons';
            font-size: 48px;
            color: #e74c3c;
            background: none;
            border: 2px solid #e74c3c;
            border-radius: 50%;
            cursor: pointer;
            transition: 0.15s linear;
        }
        
        .record_btn:hover {
            transition: 0.15s linear;
            transform: scale( 1.05 );
        }
        
        .record_btn:active {
            background: #f5f5f5;
        }
        
        .record_btn:after {
            content: '\E029';
        }
        
        .record_btn[disabled] {
            border: 2px solid #ccc;
        }
        
        .record_btn[disabled]:after {
            content: '\E02B';
            color: #ccc;
        }
        
        .record_btn[disabled]:hover {
            transition: 0.15s linear;
            transform: none;
        }
        
        .record_btn[disabled]:active {
            background: none;
        }
        
        .recording {
            animation: recording 2s infinite ease-in-out;
            position: relative;
        }
        
        .recording:before {
            content: '';
            display: inline-block;
            position: absolute;
            top: 50%;
            left: 50%;
            width: 0px;
            height: 0px;
            margin: 0px;
            border-radius: 50%;
            background: rgba( 0, 0, 0, 0.05 );
            animation: recording_before 2s infinite ease-in-out;
        }
        
        @keyframes recording {
            from {
                transform: scale( 1.1 );
            }
        
            50% {
                transform: none;
            }
        
            to {
                transform: scale( 1.1 );
            }
        }
        
        @keyframes recording_before {
            80% {
                width: 200px;
                height: 200px;
                margin: -100px;
                opacity: 0;
            }
        
            to {
                opacity: 0;
            }
        }
        
        .record_canvas {
            width: 60px;
            height: 100px;
            display: inline-block;
            
        }
        
        .txt_btn {
            color: #000;
            text-decoration: none;
            transition: 0.15s linear;
            animation: text_btn 0.3s ease-in-out;
        }
    </style>


</head>

<body>
    <div class="recorder_wrapper">
        <div class="recorder">
            <button class="record_btn" id="button"></button>
            <p id="msg_box"></p>
        </div>
    </div>

    <script>
        var msg_box = document.getElementById('msg_box'),
        button = document.getElementById('button'),
        canvas = document.getElementById('canvas'),
        lang = {
          'mic_error': '获取麦克风错误,不能在iframe中运行', //Ошибка доступа к микрофону
          'press_to_start': '点击开始录音', //Нажмите для начала записи
          'recording': '录音中', //Запись
          'play': '播放', //Воспроизвести
          'stop': '停止', //Остановить
          'download': '下载', //Скачать
          'use_https': '必须在 HTTPS下运行' },
        
        time;
        
        
        msg_box.innerHTML = lang.press_to_start;
        
        if (navigator.mediaDevices === undefined) {
          navigator.mediaDevices = {};
        }
        
        
        if (navigator.mediaDevices.getUserMedia === undefined) {
          navigator.mediaDevices.getUserMedia = function (constrains) {
            var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
            if (!getUserMedia) {
              return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
            }
        
            return new Promise(function (resolve, reject) {
              getUserMedia.call(navigator, constrains, resolve, reject);
            });
          };
        }
        
        
        if (navigator.mediaDevice.........完整代码请登录后点击上方下载按钮下载查看

网友评论0