W3cubDocs

/Web APIs

HTMLMediaElement: setSinkId() method

The HTMLMediaElement.setSinkId() method of the Audio Output Devices API sets the ID of the audio device to use for output and returns a Promise.

This only works when the application is permitted to use the specified device. For more information see the security requirements below.

Syntax

js

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 a speaker-selection Permissions Policy is used to block use of audio outputs.

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.

Security requirements

Access to the API is subject to the following constraints:

Examples

This example shows how to select an audio output device from the array returned by MediaDevices.enumerateDevices(), and set it as the sink for audio. Note that the result of enumerateDevices() only includes devices for which user permission is not required or has already been been granted.

js

const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevice = devices.find((device) => device.kind === "audiooutput");
const audio = document.createElement("audio");
await audio.setSinkId(audioDevice.deviceId);
console.log(`Audio is being output on ${audio.sinkId}`);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
setSinkId 49 17 116 No 36 No
NoNot available due to a limitation in Android.
NoNot available due to a limitation in Android.
NoNot available due to a limitation in Android (see bug 1473346).
NoNot available due to a limitation in Android.
No
NoNot available due to a limitation in Android.

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId