@custom-react-hooks/use-media-devices

中文文档
1.5.1发布于 2024/08/18MITTypeScriptCommonJS零依赖近 30 天下载 2.7万

useMediaDevices is a React hook that offers an easy way to access and monitor media devices like cameras, microphones, and speakers. It not only lists all available media input and output devices but also manages user permissions to retrieve detailed de

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Installing This Hook Individually
  4. Installing the Complete Hooks Package
  5. Importing the Hook
  6. Usage
  7. API Reference
  8. Parameters
  9. Returns object
  10. Use Cases
  11. Contributing

useMediaDevices Hook

useMediaDevices is a React hook that offers an easy way to access and monitor media devices like cameras, microphones, and speakers. It not only lists all available media input and output devices but also manages user permissions to retrieve detailed device information.

Features

  • Device Enumeration: Enumerates all available media devices with details like labels, kinds, and IDs.
  • Optional Permission Handling: Optionally prompts the user for access to media devices to retrieve full device information.
  • Loading State: Provides a loading state indicating the process of retrieving media devices.
  • Error Handling: Offers comprehensive error handling if media devices cannot be accessed or are not available.
  • Server-Side Rendering (SSR) Compatibility: Designed to be safely executed in a server-side rendering environment, gracefully handling the absence of browser-specific APIs.

Installation

Installing This Hook Individually

npm install @custom-react-hooks/use-media-devices

or

yarn add @custom-react-hooks/use-media-devices

Installing the Complete Hooks Package

npm install @custom-react-hooks/all

or

yarn add @custom-react-hooks/all

Importing the Hook

The useMediaDevices hook must be imported using a named import as shown below:

Named Import:

import { useMediaDevices } from '@custom-react-hooks/use-media-devices';

This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.

Usage

import React from 'react';
import { useMediaDevices } from '@custom-react-hooks/all';

const MediaDevicesComponent = () => {
  const { devices, isLoading, error } = useMediaDevices(false);

  if (isLoading) {
    return <div>Loading devices...</div>;
  }

  if (error) {
    return <div>Error: {error}</div>;
  }

  return (
    <div className="media-devices">
      <h2>Available Media Devices</h2>
      {devices.length === 0 ? (
        <div>No devices found.</div>
      ) : (
        <ul>
          {devices.map((device) => (
            <li key={device.id}>
              <strong>{device.kind}:</strong> {device.label}
            </li>
          ))}
        </ul>
      )}
    </div>
  );
};

export default MediaDevicesComponent;

In this example, useMediaDevices(true) is used to request user permission and list all available media devices.

API Reference

Parameters

  • requestPermission (optional, default = false): A boolean parameter that, when set to true, prompts the user for permission to access media devices such as the camera and microphone. If false, the hook will only list the available devices without requesting access.

Returns object

  • devices: An array of MediaDevice objects, each representing an available media device. Each MediaDevice object contains the following properties:
    • id: A string representing the unique identifier of the device.
    • kind: A string indicating the type of the device (e.g., 'videoinput', 'audioinput', 'audiooutput').
    • label: A string representing the label of the device, or 'Unknown Device' if the label is unavailable.
  • isLoading: A boolean indicating whether the hook is currently in the process of loading the list of media devices.
  • error: A string representing an error message if an error occurred while fetching the list of devices, or null if no error occurred.

Use Cases

  • Device Selection: Let users select from available input/output devices for media capture.
  • Feature Accessibility: Check for the availability of media devices for features like video calls.
  • Dynamic Device Updates: Update the UI when new devices are connected or disconnected.
  • Permission Management: Manage user permissions for accessing media devices.

Contributing

We welcome contributions to enhance useMediaDevices. For bug reports or feature suggestions, please open issues or submit pull requests to our repository.

版本与发布记录

@custom-react-hooks/use-media-devices 共发布 8 个版本,首次发布于 2023/12/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.1latest2024/08/182 年前
1.5.02024/08/182 年前
1.4.192023/12/092 年前
1.4.182023/12/082 年前
1.4.162023/12/072 年前
1.4.152023/12/022 年前

关于 @custom-react-hooks/use-media-devices 的常见问题

@custom-react-hooks/use-media-devices 是什么?

useMediaDevices is a React hook that offers an easy way to access and monitor media devices like cameras, microphones, and speakers. It not only lists all available media input and output devices but also manages user permissions to retrieve detailed de

如何安装 @custom-react-hooks/use-media-devices?

在项目目录运行 npm install @custom-react-hooks/use-media-devices,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@custom-react-hooks/use-media-devices 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@custom-react-hooks/use-media-devices 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@custom-react-hooks/use-media-devices 可以商用吗?

@custom-react-hooks/use-media-devices 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@custom-react-hooks/use-media-devices 的下载量有多少?

根据 npm 官方下载统计,@custom-react-hooks/use-media-devices 在 2026/10/11 – 2026/11/09 这 30 天内被下载 26,674 次,日均约 889 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@custom-react-hooks/use-media-devices 还在维护吗?

最新版本 1.5.1 发布于 2024/08/18(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。