useMediaDevices
Overview
The useMediaDevices hook simplifies access to media input devices like microphones and cameras, managing permissions and providing a way to retrieve available devices based on the specified types.
Importing
To use the useMediaDevices hook, import it:
import { useMediaDevices } from './path/to/useMediaDevices'
Hook Signature
useMediaDevices(
...xs: MediaDeviceKind[]
): [() => Promise<DeviceOptions | undefined>]
Parameters
- ...xs: A list of media device kinds to check for. Valid values:
'microphone': To check for audio input devices.'camera': To check for video input devices.
Returns
An array with a single function:
- A callback function that, when called, checks permissions and retrieves available devices for the specified media kinds.
Supporting Types
MediaDeviceKind
type MediaDeviceKind = 'microphone' | 'camera';
Represents the types of media devices that can be checked.
MediaDeviceInfoMediaDeviceSelection
type MediaDeviceInfoMediaDeviceSelection = {
deviceId: string;
label: string;
device: MediaDeviceInfo;
};
Describes a media device, including its deviceId, label, and raw MediaDeviceInfo object.
DeviceOptions
type DeviceOptions = {
audio: MediaDeviceInfoMediaDeviceSelection[];
video: MediaDeviceInfoMediaDeviceSelection[];
};
Represents the available media devices separated into audio and video categories.
How It Works
- Permissions Check:
- Uses the
navigator.permissions.queryAPI to check formicrophoneandcamerapermissions.
- Uses the
- Media Stream:
- Temporarily acquires a media stream using
navigator.mediaDevices.getUserMediato ensure devices are available.
- Temporarily acquires a media stream using
- Device Enumeration:
- Retrieves a list of available media devices with
navigator.mediaDevices.enumerateDevices.
- Retrieves a list of available media devices with
- Cleanup:
- Stops media tracks to avoid resource locking.
Example Usage
Basic Example
import React from 'react';
import { useMediaDevices } from './path/to/useMediaDevices';
const MediaDeviceComponent: React.FC = () => {
const [getDevices] = useMediaDevices('microphone', 'camera');
const [devices, setDevices] = React.useState<DeviceOptions | undefined>(undefined);
const fetchDevices = async () => {
const result = await getDevices();
setDevices(result);
};
return (
<div>
<h1>Media Devices</h1>
<button onClick={fetchDevices}>Get Devices</button>
{devices && (
<div>
<h2>Audio Devices</h2>
<ul>
{devices.audio.map((device) => (
<li key={device.deviceId}>{device.label || 'Unnamed Audio Device'}</li>
))}
</ul>
<h2>Video Devices</h2>
<ul>
{devices.video.map((device) => (
<li key={device.deviceId}>{device.label || 'Unnamed Video Device'}</li>
))}
</ul>
</div>
)}
</div>
);
};
export default MediaDeviceComponent;
Explanation
- Media Device Check: The
getDevicesfunction is called to retrieve audio and video devices. - State Update: The results are stored in
devicesand displayed in lists for audio and video devices. - Dynamic Rendering: The UI updates based on the availability of media devices.
Best Practices
- Handle Permissions Gracefully: Check for denied permissions and inform users if access is not granted.
- Device Refresh: Provide a way to refresh the list of devices as they may change dynamically (e.g., USB device plugged in).
- Error Handling: Use
try-catchto manage potential errors when accessing media devices.
Notes
- The
checkPermissionshelper function is used internally to validate permissions and gather devices. - Media tracks are stopped after use to prevent resource locking.
Contributing
If you encounter issues or have suggestions for improving the useMediaDevices hook, feel free to contribute or report an issue on the repository.