Last active
July 25, 2020 20:24
-
-
Save 1Marc/1a59bb254637aaf399d9f500cbe297fd to your computer and use it in GitHub Desktop.
Change Audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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