Skip to content

Instantly share code, notes, and snippets.

@1Marc
Last active July 25, 2020 20:24
Show Gist options
  • Save 1Marc/1a59bb254637aaf399d9f500cbe297fd to your computer and use it in GitHub Desktop.
Save 1Marc/1a59bb254637aaf399d9f500cbe297fd to your computer and use it in GitHub Desktop.
Change Audio
function gotDevices(deviceInfos) {
changeAudioDestination(deviceInfos[1]) // whatever device
}
function changeAudioDestination(audioInput) {
attachSinkId(document.getElementsByTagName('video')[0], audioInput);
}
function attachSinkId(element, sinkId) {
if (typeof element.sinkId !== 'undefined') {
element.setSinkId(sinkId)
.then(() => {
console.log(`Success, audio output device attached: ${sinkId}`);
})
.catch(error => {
let errorMessage = error;
if (error.name === 'SecurityError') {
errorMessage = `You need to use HTTPS for selecting audio output device: ${error}`;
}
console.error(errorMessage);
});
} else {
console.warn('Browser does not support output device selection.');
}
}
navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment