最近有做到检测音视频的功能,记录一下,在网上也没有找到合适的文章讲解如何切换。
获取扬声器设备也是使用的 navigator.mediaDevices.enumerateDevices() 方法
1.获取到扬声器设备列表,代码如下 其中 audiooutput 就是代表的扬声器设备。
const [audioList, setAudioList] = useState([]);
//获取音频列表
const getSpeakerList = () => {
navigator.mediaDevices
.enumerateDevices()
.then(function (devices) {
let list = [];
devices.forEach(function (device) {
if (device.kind === 'audiooutput') {
device.value=device.deviceId
list.push(device);
}
});
setAudioList(list);
})
.catch(function (err) {
console.log(err.name + ': ' + err.message);
});
};
2.进行切换,主要是使用到了一个新的方法API setSinkId
附上方法地址:https://deve