1.0.3 • Published 3 months ago

use-media-stream v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Use Media Stream

use-media-stream is a powerful React hook designed to streamline the integration and management of media streams within your React applications. It offers a comprehensive set of features and options for effortless control and manipulation of media streams. It provides a convenient interface for handling media devices, initiating media streams, and controlling audio and video tracks.

GitHub License

Demo - Link

Installation

Install the hook using your preferred package manager:

npm install use-media-stream
or
yarn add use-media-stream

Usage

Import the hook into your React component and leverage its capabilities to manage your media streams:

import useMediaStream from 'use-media-stream';

function MyComponent() {
  const {
    stream,
    isSupported,
    isStreaming,
    isAudioMuted,
    isVideoMuted,
    // ... other properties and handlers
  } = useMediaStream();

  // ... your component logic
}

Props Values

PropTypeDescription
mediaDeviceConstraintsMediaStreamConstraints \| nullThe constraints for the media device to be used in the media stream.

Return Values

PropertyTypeDescription
streamMediaStream \| nullThe current media stream object.
isStreamingbooleanIndicates whether the media stream is currently active.
isAudioMutedbooleanIndicates whether audio tracks are muted.
isVideoMutedbooleanIndicates whether video tracks are muted.
devicesMediaDeviceInfo[]An array of available media devices.
audioInputDevicesMediaDeviceInfo[]An array of available audio input devices.
audioOutputDevicesMediaDeviceInfo[]An array of available audio output devices.
videoInputDevicesMediaDeviceInfo[]An array of available video input devices.
getStreamRequestREQUEST_STATESThe state of the request to obtain the media stream (IDLE, PENDING, FULFILLED, or REJECTED).
getMediaDevicesRequestREQUEST_STATESThe state of the request to obtain media devices (IDLE, PENDING, FULFILLED, or REJECTED).
errorunknownAny error that occurred during media stream or device retrieval.
start() => Promise<MediaStream \| null>Initiates the media stream if not already streaming.
stop() => voidStops the media stream if currently streaming.
getMediaDevices() => Promise<MediaDeviceInfo[]>Retrieves a list of available media devices.
updateMediaDeviceConstraints({ constraints, resetStream }) => Promise<void>Updates media device constraints and optionally resets the media stream.
muteAudio() => voidMutes all audio tracks in the media stream.
unmuteAudio() => voidUnmutes all audio tracks in the media stream.
muteVideo() => voidMutes all video tracks in the media stream.
unmuteVideo() => voidUnmutes all video tracks in the media stream.
addVideoEndedEventListener(fn: EventListenerOrEventListenerObject) => voidAdds an event listener for 'ended' events on video tracks.
addAudioEndedEventListener(fn: EventListenerOrEventListenerObject) => voidAdds an event listener for 'ended' events on audio tracks.
addVideoMuteEventListener(fn: EventListenerOrEventListenerObject) => voidAdds an event listener for 'mute' events on video tracks.
addAudioMuteEventListener(fn: EventListenerOrEventListenerObject) => voidAdds an event listener for 'mute' events on audio tracks.
removeVideoEndedEventListener(fn: EventListenerOrEventListenerObject) => voidRemoves an event listener for 'ended' events on video tracks.
removeAudioEndedEventListener(fn: EventListenerOrEventListenerObject) => voidRemoves an event listener for 'ended' events on audio tracks.
removeVideoMuteEventListener(fn: EventListenerOrEventListenerObject) => voidRemoves an event listener for 'mute' events on video tracks.
removeAudioMuteEventListener(fn: EventListenerOrEventListenerObject) => voidRemoves an event listener for 'mute' events on audio tracks.

Example

Demo - Link

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

Feel free to use and contribute! If you encounter any issues or have suggestions, please open an issue.