Skip to main content

Why can't I get the device ID on Chrome 81?

On Chrome 81, the deviceId field that the getDevices method returns is empty.

You may also encounter this issue on Safari and Firefox.

Reason

As of Chrome 81, device IDs are not exposed if permission to use devices has not been granted. This change is to protect user privacy.

For the Agora Web SDK, if you get device information before the method call of Stream.init succeeds, the returned deviceId may be empty.

Solution

You can create temporary streams for media capture to trigger the device permission request and get the device information.

  1. Based on your requirements, create temporary streams for audio or video capture to trigger requests for microphone or camera permissions.


    _2
    const tempAudioStream = AgoraRTC.createStream({ audio: true, video: false });
    _2
    const tempVideoStream = AgoraRTC.createStream({ audio: false, video: true });

  2. After the streams are initialized, call getDevices to get the complete device information.

    Note

    • The capture may fail if no microphone or camera exists, or if the user has disabled these devices. The capture is a workaround and you only need to ensure that the failure does not stop the code execution.

    • If the capture fails, you may still get empty deviceId and label fields in the returned device list.


    _28
    const audioPermissionOK = new Promise(resolve => {
    _28
    tempAudioStream.init(
    _28
    () => resolve(null),
    _28
    e => resolve(e)
    _28
    );
    _28
    });
    _28
    const videoPermissionOK = new Promise(resolve => {
    _28
    tempVideoStream.init(
    _28
    () => resolve(null),
    _28
    e => resolve(e)
    _28
    );
    _28
    });
    _28
    _28
    Promise.all([audioPermissionOK, videoPermissionOK]).then(res => {
    _28
    if (res[0] !== null) {
    _28
    console.warn('create audio temp stream failed!', res[0]);
    _28
    }
    _28
    if (res[1] !== null) {
    _28
    console.warn('create video temp stream failed!', res[0]);
    _28
    }
    _28
    _28
    // After the capture, call getDevices to get device information
    _28
    const devices = AgoraRTC.getDevices(info => {
    _28
    tempAudioStream.close();
    _28
    tempVideoStream.close();
    _28
    console.log('get device info!', info);
    _28
    });
    _28
    });

vundefined