JS_0025:Promise 异步编程

1,

    listCamera(videoDevice) {
        return new Promise((resolve, reject) => {
            navigator.mediaDevices.enumerateDevices()
                .then((devices) => {
                let index = 1;
                devices.find((device) => {
                    if (device.kind === 'videoinput') {
                        const option = document.createElement('option');
                        // 在iOS12.2上deviceId为空
                        if (device.deviceId == '') {
                            option.text = device.label || 'camera ' + this.cameras[index];
                            option.value = JSON.stringify({ audio: false, video: { facingMode: { exact: this.cameras[index] } } });
                            index++;
                        }
                        else {
                            option.text = device.label || 'camera ' + (videoDevice.length + 1).toString();
                            option.value = JSON.stringify({ audio: false, video: { deviceId: { exact: device.deviceId } } });
                        }
                        // 将摄像头信息存储在select元素中,方便切换前、后置摄像头
                        videoDevice.appendChild(option);
                    }
                    return false;
                });
                if (videoDevice.length === 0) {
                    console.log("list1");
                    reject('没有可使用的视频设备');
                }
                else {
                    this.initVideo();
                    this.initCanvas();
                    resolve(true);
                }
            }).catch(err => {
                reject(err);
            });
        });
    }






 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2020-08-19 16:49  琥珀君  阅读(157)  评论(0编辑  收藏  举报