useUserMedia
Installation
npx sse-hooks add use-user-media
yarn dlx sse-hooks add use-user-media
pnpm dlx sse-hooks add use-user-media
deno run -A npm:sse-hooks add use-user-media
bunx sse-hooks add use-user-media
Usage
import { useUserMedia } from "./{hooks file}";
const { stream, error, startCapture, stopCapture } = useUserMedia();
return (
<div>
{error && <p>Error: {error}</p>}
<video
autoPlay
muted
playsInline
ref={(node) => {
if (node && stream) node.srcObject = stream;
}}
/>
<button onClick={() => startCapture()}>Start Camera</button>
<button onClick={stopCapture}>Stop Camera</button>
</div>
);
API
Parameters
| Prop | Default | Type |
|---|---|---|
initialConstraints |
|
The initial constraints for audio and video.
|
Returns
| Return Value | Default | Type |
|---|---|---|
error | - |
An error message string if the media capture failed, or null otherwise. |
isLoading | - |
A boolean indicating if the media stream is currently loading (requesting permission). |
isSupported | - |
A boolean indicating if the |
startCapture | - |
Function to start capturing media. Accepts optional constraints to override defaults. |
stopCapture | - |
Function to stop the current media capture and release tracks. |
stream | - |
The current MediaStream object, or null if no stream is active. |