Microphone Passthrough to Bluetooth Speaker
Start Passthrough Stop PassthroughPress “Start Passthrough” to begin.
let audioContext; let microphone; let audioSource; const startBtn = document.getElementById(‘startBtn’); const stopBtn = document.getElementById(‘stopBtn’); const status = document.getElementById(‘status’); // Function to start audio passthrough startBtn.addEventListener(‘click’, async () => { try { // Request access to the microphone const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); audioContext = new (window.AudioContext || window.webkitAudioContext)(); microphone = audioContext.createMediaStreamSource(stream); // Connect microphone directly to the audio context’s destination (e.g., speakers) microphone.connect(audioContext.destination); status.textContent = ‘Passthrough active. Sound from the microphone is playing on your speakers.’; startBtn.disabled = true; stopBtn.disabled = false; } catch (error) { console.error(‘Error accessing microphone:’, error); status.textContent = ‘Error accessing microphone. Please check permissions.’; } }); // Function to stop audio passthrough stopBtn.addEventListener(‘click’, () => { if (microphone) { microphone.disconnect(); audioContext.close(); status.textContent = ‘Passthrough stopped.’; startBtn.disabled = false; stopBtn.disabled = true; } });