WebRTC - 更改音频输出设备



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

并在选择所需的设备时尝试执行此操作:

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(例如:b6d4b95564be24f8be5b8e982fdf851e658a05bb9d3aeec09280976b11debc2a(

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

然而,enumerateDevices函数的deviceId结果:

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

此外,您应该检查此媒体元素播放的 MediaStream 是否具有音轨,否则将触发异常。

相关内容

  • 没有找到相关文章

最新更新