abilitytest.js 文件
在webrtc 项目中 需要判断浏览器是否支持 调用 视屏音频 功能
import {
message
} from 'antd';
// 这种方式在各个浏览器都可以(不过在Safari和Firefox浏览器获取的设备信息和在Chrome获取的信息还是不一样的)
export const abilitytest = async () => {
let isSupport = true
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
console.log('不支持获取设备信息!');
message.warning(
'当前浏览器不支持获取设备信息!请及时更换浏览器~'
);
} else {
message.info(
'系统正在获取你的设备信息~'
);
// 首先获取到流,获取流成功后再获取设备信息
await navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(gotMediaStream).then(gotDevices).catch(handleError);
}
// 采集音视频数据成功时调用的方法,获取设备信息
function gotMediaStream(stream: any) {
// 为了及时关闭摄像头
stream.getTracks().forEach(function (track: { stop: () => void; }) {
track.stop();
});
return navigator.mediaDevices.enumerateDevices();
}
// 浏览器获取音视频设备成功时调用的方法
function gotDevices(deviceInfos: any[]) {
deviceInfos.forEach(function (deviceInfo: { kind: string; label: string; deviceId: string; groupId: string; }) {
console.log('设备种类=' + deviceInfo.kind + ':设备名 = ' + deviceInfo.label + ';设备id = ' + deviceInfo.deviceId + ';groupId=' + deviceInfo.groupId);
})
}
// 浏览器获取音视频设备失败时调用的方法
function handleError(err: { name: string; message: string; }) {
console.log(err.name + ':' + err.message);
isSupport = false
}
return isSupport
}