Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't wait for prompt #1

Open
matthewdfleming opened this issue Mar 22, 2020 · 2 comments
Open

Doesn't wait for prompt #1

matthewdfleming opened this issue Mar 22, 2020 · 2 comments

Comments

@matthewdfleming
Copy link

Hey there.. love this little bit of code, super handy. One thing I noticed is that the code doesn't really wait for the getUserMedia() call to complete before calling enumerateDevices().. so my phone wasn't enabling both cameras because I hadn't accepted the use prior to the looping over the enumeration.

I would have created a patch but was messing around so much that the code looks kinda horrible.. instead here's the relevant parts:

        function successCallback(stream) {
            currentStream = stream;
            video.srcObject = stream;
            video.play();
            if (location.href.includes('&debug')) {
                console.log(`stream: ${stream}`);
            }
// PATCH: return the enumerate here as a promise..
            return navigator.mediaDevices.enumerateDevices();
        }

        function errorCallback(error) {
            window.alert('Error: ', error);
        }

        navigator.mediaDevices.getUserMedia({audio: false, video: true})
            .then(successCallback)
// PATCH: so we can then after the prompt, enumerate the devices
            .then(media_devices => {
                media_devices.forEach(media_device => {
                    if (location.href.includes('&debug')) {
                        console.log(media_device);
                    }
                    if (media_device.kind === 'videoinput') {
                        cameras = cameras.concat(media_device.deviceId);
                    }
                })
            })
            .catch(errorCallback);
@matthewdfleming
Copy link
Author

Obviously you can close this 'issue' but just in case anyone else uses your great sample, they know why the rear camera doesn't seem to work.

simonmaris added a commit to simonmaris/camera-viewer that referenced this issue Apr 1, 2020
@TrevCan
Copy link

TrevCan commented Jul 20, 2021

Hey @matthewdfleming I've added your patch into my fork repo, it works great now! (https://trevcan.github.io/camera-viewer) Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants