use-user-media-hook
v1.1.3
Published
Hook to access user audio and video devices
Maintainers
Readme
use-user-media-hook
A React hook for accessing user audio and video devices via the MediaStream API. Resource cleanup is handled automatically on unmount. This hook requires no additional dependencies.
Installation
npm install use-user-media-hookUsage
import { useUserMedia } from 'use-user-media-hook';
function VideoApp() {
const {
stream,
mediaAvailable,
isLoading,
error,
getUserMediaStream,
stopUserMediaStream,
} = useUserMedia();
return (
<div>
<button onClick={() => getUserMediaStream({ audio: true, video: true })}>
Start Stream
</button>
<button onClick={stopUserMediaStream}>Stop Stream</button>
{error && <p>Error: {error.message}</p>}
{isLoading && <p>Loading...</p>}
</div>
);
}API
useUserMedia(): UseUserMediaReturn
Returns an object with the following properties:
stream: MediaStream | null- The active media streammediaAvailable: boolean- Whether media devices are availableisLoading: boolean- Loading state while requesting permissionserror: Error | null- Any errors that occurredhasAudio: boolean- Whether audio tracks are presenthasVideo: boolean- Whether video tracks are presentisAudioEnabled: boolean- Whether audio is currently enabledisVideoEnabled: boolean- Whether video is currently enabledgetUserMediaStream(constraints?: MediaStreamConstraints): Promise<void>- Request media stream, defaults to request 1080p video and audio if no constraints are providedstopUserMediaStream(): void- Stop and release the media streamsetAudioState(enabled: boolean): void- Toggle audio trackssetVideoState(enabled: boolean): void- Toggle video tracks
More details on constraints that can be passed can be found here
Development
Build
npm run buildLint
npm run lint
npm run lint:fixFormat
npm run formatTesting
npm run testCI/CD
This project uses GitHub Actions for continuous integration and automated releases:
- CI Workflow (
.github/workflows/ci.yml): Runs linting and builds on pull requests and pushes tomainanddevelopbranches - Release Workflow (
.github/workflows/release.yml): Automatically publishes releases to npm using semantic-release
Release Process
Releases are automatically managed by semantic-release based on commit messages:
feat:- Minor version bump (features)fix:- Patch version bump (bug fixes)BREAKING CHANGE:- Major version bump
License
MIT
