WebRTC - 更改音频输出设备

时间:2017-07-17 14:00:54

标签: javascript webrtc getusermedia

我有一个使用用户媒体设备的应用程序,我想允许用户自己更改音频输出。为此,我遵循了this example

当我选择我想要的设备时尝试这样做:

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是HTML文件中的音频标记。 deviceId是我想用作输出的设备的ID( ex:b6d4b95564be24f8be5b8e982fdf851e658a05bb9d3aeec09280976b11debc2a

但是这给了我一个 DOMException error : Requested device not found

然而deviceId函数的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");
});

那有什么不对?

修改

这就是我如何获取用户媒体和设备列表:

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);

0 个答案:

没有答案