How to use JavaScript to switch between phone speaker and handset

problem description

recently, when developing webrtc, there was a need for customers to switch speakers and handsets on the web page

related codes

found a lot of information on the Internet
navigator.mediaDevices.enumerateDevices (). Then (gotDevices)
function gotDevices (mediaDevices) {

    mediaDevices.forEach((mediaDevice) => {
        if (mediaDevice.kind === "audioinput") {

        }
    });
}

I got the audio output device in this way, but in fact, I only got one or only one device with an id of default.
is there something wrong with my writing method? If I can find more devices, how can I call the new device?

May.05,2022

https://github.com/webrtc/sam...
has found demo, which can be realized by testing, but it seems that some models do not support it. Self-test

Menu