WebRTC: (1)設備管理

重要的API: enumerateDevices

基本格式:

其中ePromise中有個重要的結構體:MediaDevicesInfo


實操:

先準備express 部署一個Web服務器:

然後啓動web服務器:

就可以訪問public下發布路徑下的文件了:

 這樣就進入了設備管理準備的代碼:

案例1: 打印設備信息

 

 其中的label沒顯示是因爲http和https不安全的訪問具有安全性問題,所以不會顯示出來,

即使我這裏使用的是https,但是

證書是自己的

可以通過letsencryp網站進行解決。

案例2: 在頁面中顯示設備

注意這裏需要處理獲取認證證書不安全的問題,否則這裏的label爲空:

client.js:

var audioSource = document.querySelector('select#audioSource');
var audioOutput = document.querySelector('select#audioOutput');
var videoSource = document.querySelector('select#videoSource');

if(!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices){
    console.log('enumerateDevices are not supported!');
}else{
    navigator.mediaDevices.enumerateDevices()
        .then(gotDevices)
        .catch(handleError);
}

function gotDevices(deviceInfos){
    deviceInfos.forEach( function(deviceInfo){
        console.log(deviceInfo.kind + ": label = " +deviceInfo.label + ": id = " +deviceInfo.deviceId + ": groupId = " + deviceInfo.groupId);

        var option = document.createElement('option');
        option.text = deviceInfo.label;
        option.value = deviceInfo.deviceId;
        if(deviceInfo.kind === 'audioinput'){
            audioSource.appendChild(option);
        }else if(deviceInfo.kind === 'audiooutput'){
            audioOutput.appendChild(option);
        }else if(deviceInfo.kind === 'videoinput'){
            videoSource.appendChild(option);
        }
    });
}

function handleError(err){
    console.log(err.name + ":" + err.message);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章