WebRTC - change audio output device

I have an application using custom multimedia devices and I would like users to change the audio output sound. To do this I followed in this example

And try this when I select the device I want:

var deviceId = "b6d4b95564be24f8be5b8e982fdf851e658a05bb9d3aeec09280976b11debc2a";
var audioElement = document.querySelector('audio');
if (typeof audioElement.sinkId !== 'undefined') {
    audioElement.setSinkId(deviceId)
        .then(function() {
            console.log('Success, audio output device attached: ' + deviceId);
        })
        .catch(function(error) {
            var 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.');
}

      

audioElement

is a sound tag in an HTML file. deviceId

is the ID of the device that I want to use as output (for example: b6d4b95564be24f8be5b8e982fdf851e658a05bb9d3aeec09280976b11debc2a)

But it gives me . DOMException error : Requested device not found

However, the result of the deviceId

function is enumerateDevices

:

navigator.mediaDevices.enumerateDevices().then(function(devices){
    var audioInputList = _.filter(devices, function(device){ return device.kind == "audioinput"; });
    var audioOutputList = _.filter(devices, function(device){ return device.kind == "audiooutput"; });
}).catch(function(){
    console.log("error");
});

      

So what's wrong?

EDIT

This is how I get the list of UserMedia and devices:

function handleSuccess(stream){
    var audioElement = document.querySelector('audio');
    audioElement.srcObject = stream;
    return navigator.mediaDevices.enumerateDevices();
}

function gotDevices(devices){
    var audioDevice = { input:[], output:[] };
    audioDevice.input = _.filter(devices, function(device){ return device.kind == "audioinput"; });
    audioDevice.output = _.filter(devices, function(device){ return device.kind == "audiooutput"; });
}

function start(contraints){
    deconnectPeerConnection();
    navigator.mediaDevices.getUserMedia(contraints).then(handleSuccess)
    .then(gotDevices).catch(handleError);
}
start(constraints);

      

+3


source to share





All Articles