In this episode, we take what we've done so far with our state management and use it to add screen capture functionality. We start by creating a new "Capture Screen" button that the user can click. Behind the scenes, this triggers a new method, which is almost identical to the way we captured webcam video — with one key difference: instead of using getUserMedia
, we're using getDisplayMedia
to capture the user's screen.
We also make sure to turn off audio for this capture, since we'll be handling audio separately. The captured screen stream gets added to our state just like the camera stream did, which means all the logic for updating the UI and showing a video preview works just as smoothly as before.
We finish up by testing everything out — clicking the button, approving the browser prompt, and verifying that the preview works perfectly. It's a really clean implementation thanks to the groundwork we've already laid, and it's cool to see how easily we can switch between capturing the webcam or the screen by just switching up which API method we call!