A browser speaker test has one classic failure mode: the user runs it while a pair of Bluetooth earbuds is still connected, hears nothing from the phone, and concludes the speaker is dead. The Media Devices API can catch most of these cases before the test even starts.
Listing audio outputs
navigator.mediaDevices.enumerateDevices() returns every input and output device the browser knows about. Audio outputs have kind === 'audiooutput':
async function audioOutputs() {
const devices = await navigator.mediaDevices.enumerateDevices();
return devices.filter(d => d.kind === 'audiooutput');
}
There is a catch: until the page has some media permission, browsers hide device labels and may return only a generic default entry. That is a privacy measure, so do not rely on labels to identify a headset.
Watching for changes
The more useful signal is the devicechange event, which fires when a device is plugged in, paired or removed:
navigator.mediaDevices.addEventListener('devicechange', async () => {
const outs = await audioOutputs();
showNotice(`Audio outputs changed (${outs.length} available). ` +
'If you connected headphones, the test will play there, not on the speaker.');
});
Show the notice while the test is open. A user who pairs earbuds mid-test gets an immediate explanation instead of silence.
A simple pre-test prompt
Because labels are often hidden, the most reliable approach is to ask, using what you can detect as a hint:
async function preflight() {
const outs = await audioOutputs();
if (outs.length > 1) {
return confirm('More than one audio output is available. ' +
'Disconnect Bluetooth or wired headphones so the test plays on the built-in speaker. Continue?');
}
return true;
}
More than one output does not always mean headphones are active (some laptops list HDMI or virtual devices), so treat it as a reason to ask, not a verdict.
Choosing the output explicitly
Where supported, HTMLMediaElement.setSinkId(deviceId) and AudioContext.setSinkId() let a page send sound to a specific output after the user grants permission. Support varies by browser and platform, and mobile browsers usually route audio at the OS level anyway, so always keep a manual fallback: tell the user to check the output switcher in their system media controls.
Small UX details that help
- Play a short, quiet chime first and ask "Did you hear this from the phone itself?" before running the full test.
- Remember the answer for the session so the prompt does not repeat on every test.
- If the user says no, show the two most common causes: headphones still connected, or the media volume (not the ringer volume) turned down.
If you would rather use a finished set of browser speaker tools than build one, Fix My Speaker has free cleaning tones and speaker tests.
Reference: MDN on MediaDevices: devicechange event, https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/devicechange_event
Top comments (0)