Size
0.57 kb
useUserMedia
Access and manage media devices like camera and microphone.
Installation
Usage
Pass the media constraints to the hook.
The hook follows a simple state machine:
- Initially starts in
"closed"
state withstream: null
- Calling
open()
transitions to"loading"
state while requesting device access - On successful device access, transitions to
"open"
state withstream: MediaStream
- If an error occurs during loading, transitions to
"error"
state with the error details - Calling
close()
returns to"closed"
state, stops the stream, and setsstream: null
This predictable state flow makes it easy to build UIs that respond appropriately to the media stream's status.
Example
Capture and display video from the device's camera.
Parameters
UseUserMediaOptions
The options of the useUserMedia hook. See MDN docs for more details.
Prop | Type | Default |
---|---|---|
video? | boolean | MediaTrackConstraints | - |
preferCurrentTab? | boolean | - |
peerIdentity? | string | - |
audio? | boolean | MediaTrackConstraints | - |
UseUserMediaReturn
The return value of the useUserMedia hook.
Prop | Type | Default |
---|---|---|
toggleVideo | () => void | - |
toggleAudio | () => void | - |
close | () => void | - |
open | () => void | - |
videoState | "none" | "enabled" | "disabled" | - |
audioState | "none" | "enabled" | "disabled" | - |
error | Error | null | - |
state | "closed" | "error" | "open" | "loading" | - |
stream | MediaStream | null | - |