I'm working on an audio recording feature. I usually work with my macbook closed and noticed a bug where if the mac is closed, the built-in microphone still shows up as an audio input but when i record i just get a stream of zeros.
I'm trying to test for this before recording. I do this to get a list of audio inputs:
await navigator.mediaDevices.getUserMedia({ audio: true });
const devices = await navigator.mediaDevices.enumerateDevices();
const audioInputs = devices.filter(device => device.kind === 'audioinput');navigator.mediaDevices.getUserMedia({ audio: true });
At this point, audioInputs looks like this:
(2) [InputDeviceInfo, InputDeviceInfo]
0: InputDeviceInfo {
deviceId: '1da38adda99ecb1795a4860369035175feae346e3793be1ed725f17e9a67d592',
kind: 'audioinput',
label: 'MacBook Air Microphone (Built-in)',
groupId: 'd7b07897adfabc32abc58c44d372664304e0f53d3590f47f8fafc02615a62de8'
}
1: InputDeviceInfo {
deviceId: 'default',
kind: 'audioinput',
label: 'Default - MacBook Air Microphone (Built-in)',
groupId: 'd7b07897adfabc32abc58c44d372664304e0f53d3590f47f8fafc02615a62de8'
}
length: 2
[[Prototype]]: Array(0)
I don't know why it's showing up twice but i don't mind that for now. What i want to know is how i can tell if one of these is actually on, ie is going to actually record some data.
I thought about doing a short test recording and seeing if the audio stream is all zeros, but i thought there might be a cleaner way to find out in advance. Any suggestions welcome.