1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Grabador de Voz</title> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container mt-5"> <h1 class="text-center mb-4">Grabador de Voz</h1> <div class="d-flex justify-content-center mb-3"> <button id="startBtn" class="btn btn-primary me-2">Iniciar Grabación</button> <button id="stopBtn" class="btn btn-danger me-2" disabled>Detener Grabación</button> <button id="playBtn" class="btn btn-success me-2" disabled>Reproducir Audio</button> <button id="downloadBtn" class="btn btn-info" disabled>Descargar Audio</button> </div> <div class="d-flex justify-content-center"> <audio id="audioPlayback" controls class="w-75"></audio> </div> </div> <!-- Bootstrap JS (Popper.js incluido) --> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> <script> let mediaRecorder; let audioChunks = []; const startBtn = document.getElementById('startBtn'); const stopBtn = document.getElementById('stopBtn'); const playBtn = document.getElementById('playBtn'); const downloadBtn = document.getElementById('downloadBtn'); const audioPlayback = document.getElementById('audioPlayback'); let audioBlob; startBtn.addEventListener('click', async () => { audioChunks = []; const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' }); mediaRecorder.ondataavailable = event => { audioChunks.push(event.data); }; mediaRecorder.onstop = () => { audioBlob = new Blob(audioChunks, { type: 'audio/webm' }); const audioUrl = URL.createObjectURL(audioBlob); audioPlayback.src = audioUrl; playBtn.disabled = false; downloadBtn.disabled = false; }; mediaRecorder.start(); startBtn.disabled = true; stopBtn.disabled = false; }); stopBtn.addEventListener('click', () => { mediaRecorder.stop(); startBtn.disabled = false; stopBtn.disabled = true; }); playBtn.addEventListener('click', () => { audioPlayback.play(); }); downloadBtn.addEventListener('click', () => { const downloadLink = document.createElement('a'); const url = URL.createObjectURL(audioBlob); downloadLink.href = url; downloadLink.setAttribute('download', 'grabacion.webm'); document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); URL.revokeObjectURL(url); // Revocamos la URL para liberar memoria }); </script> </body> </html> |