From 23d563b97a3d98da5103a70ebbb73bc0c4594ae8 Mon Sep 17 00:00:00 2001 From: Jose B Date: Fri, 21 Jul 2023 07:42:17 -0500 Subject: [PATCH] handle audio input permissions --- app/components/record.js | 63 ++++++++++++++++++++++++++-------------- app/page.js | 2 +- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/app/components/record.js b/app/components/record.js index 6079a920..92d339aa 100644 --- a/app/components/record.js +++ b/app/components/record.js @@ -7,6 +7,46 @@ import "react-dropdown/style.css"; import CustomRecordPlugin from "./CustomRecordPlugin"; +const queryAndPromptAudio = async () => { + const permissionStatus = await navigator.permissions.query({name: 'microphone'}) + if (permissionStatus.state == 'prompt') { + await navigator.mediaDevices.getUserMedia({ audio: true }) + } +} + +const AudioInputsDropdown = (props) => { + const [ddOptions, setDdOptions] = useState([]); + + useEffect(() => { + const init = async () => { + await queryAndPromptAudio() + + const devices = await navigator.mediaDevices.enumerateDevices() + const audioDevices = devices + .filter((d) => d.kind === "audioinput" && d.deviceId != "") + .map((d) => ({ value: d.deviceId, label: d.label })) + + if (audioDevices.length < 1) return console.log("no audio input devices") + + setDdOptions(audioDevices) + props.setDeviceId(audioDevices[0].value) + } + init() + }, []) + + const handleDropdownChange = (e) => { + props.setDeviceId(e.value); + }; + + return ( + + ) +} + export default function Recorder(props) { const waveformRef = useRef(); const [wavesurfer, setWavesurfer] = useState(null); @@ -14,22 +54,10 @@ export default function Recorder(props) { const [isRecording, setIsRecording] = useState(false); const [isPlaying, setIsPlaying] = useState(false); const [deviceId, setDeviceId] = useState(null); - const [ddOptions, setDdOptions] = useState([]); useEffect(() => { document.getElementById("play-btn").disabled = true; - navigator.mediaDevices.enumerateDevices().then((devices) => { - const audioDevices = devices - .filter((d) => d.kind === "audioinput") - .map((d) => ({ value: d.deviceId, label: d.label })); - - if (audioDevices.length < 1) return console.log("no audio input devices"); - - setDdOptions(audioDevices); - setDeviceId(audioDevices[0].value); - }); - if (waveformRef.current) { const _wavesurfer = WaveSurfer.create({ container: waveformRef.current, @@ -84,22 +112,15 @@ export default function Recorder(props) { wavesurfer?.playPause(); }; - const handleDropdownChange = (e) => { - setDeviceId(e.value); - }; - return (
- +   diff --git a/app/page.js b/app/page.js index c0fbfa10..e0ef15f9 100644 --- a/app/page.js +++ b/app/page.js @@ -19,7 +19,7 @@ const App = () => {

Capture The Signal, Not The Noise

- serverData.peer.send(JSON.stringify({ cmd: 'STOP' }))}/> + serverData?.peer?.send(JSON.stringify({ cmd: 'STOP' }))}/>