HTMLMediaElement.setSinkId()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The HTMLMediaElement.setSinkId() method sets the ID of the audio device to use for output and returns a Promise. This only works when the application is authorized to use the specified device.

Syntax

setSinkId(sinkId)

Parameters

sinkId

The MediaDeviceInfo.deviceId of the audio output device.

Return value

A Promise that resolves to undefined.

Exceptions

NotAllowedError DOMException

Returned if there is no permission to use the requested device.

NotFoundError DOMException

Returned if the deviceId does not match any audio output device.

AbortError DOMException

Returned if switching the audio output device to the new audio device failed.

Examples

const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevices = devices.filter((device) => device.kind === 'audiooutput');
const audio = document.createElement('audio');
await audio.setSinkId(audioDevices[0].deviceId);
console.log(`Audio is being played on ${audio.sinkId}`);

Specifications

Specification
Audio Output Devices API
# dom-htmlmediaelement-setsinkid

Browser compatibility

BCD tables only load in the browser