Skip to content

useDevicesList

类别
导出大小
1.2 kB
最后更改
5 天前
相关

响应式的 enumerateDevices 列出可用的输入/输出设备。

演示

摄像头 (0)
麦克风 (0)
扬声器 (0)

用法

js
import { useDevicesList } from '@vueuse/core'

const {
  devices,
  videoInputs: cameras,
  audioInputs: microphones,
  audioOutputs: speakers,
} = useDevicesList()

请求权限

要请求权限,请使用 ensurePermissions 方法。

js
const {
  ensurePermissions,
  permissionGranted,
} = useDevicesList()

await ensurePermissions()
console.log(permissionsGranted.value)

组件

vue
<template>
  <UseDevicesList v-slot="{ videoInputs, audioInputs, audioOutputs }">
    Cameras: {{ videoInputs }}
    Microphones: {{ audioInputs }}
    Speakers: {{ audioOutputs }}
  </UseDevicesList>
</template>

类型声明

显示类型声明
typescript
export interface UseDevicesListOptions extends ConfigurableNavigator {
  onUpdated?: (devices: MediaDeviceInfo[]) => void
  /**
   * Request for permissions immediately if it's not granted,
   * otherwise label and deviceIds could be empty
   *
   * @default false
   */
  requestPermissions?: boolean
  /**
   * Request for types of media permissions
   *
   * @default { audio: true, video: true }
   */
  constraints?: MediaStreamConstraints
}
export interface UseDevicesListReturn {
  /**
   * All devices
   */
  devices: Ref<MediaDeviceInfo[]>
  videoInputs: ComputedRef<MediaDeviceInfo[]>
  audioInputs: ComputedRef<MediaDeviceInfo[]>
  audioOutputs: ComputedRef<MediaDeviceInfo[]>
  permissionGranted: ShallowRef<boolean>
  ensurePermissions: () => Promise<boolean>
  isSupported: ComputedRef<boolean>
}
/**
 * Reactive `enumerateDevices` listing available input/output devices
 *
 * @see https://vueuse.org.cn/useDevicesList
 * @param options
 */
export declare function useDevicesList(
  options?: UseDevicesListOptions,
): UseDevicesListReturn

源码

SourceDemoDocs

贡献者

Anthony Fu
wheat
Nikitatopodin
IlyaL
Fernando Fernández
Alex Liu
Klein Petr
Espen Solli Grande
Anthony Fu
Félix Zapata
vaakian X
vaakian X
Jelf
Andras Serfozo
Alex Kozack

更新日志

v12.8.0 on 3/5/2025
f9685 - fix(useDeviceList): 如果摄像头权限为 granted,则 audioInputs 不会更新 (#4559)
v12.4.0 on 1/10/2025
dd316 - feat: 尽可能在任何地方使用被动事件处理程序 (#4477)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: 删除 Vue 2 支持,优化 bundles 并清理 (#4349)
v11.2.0 on 10/30/2024
bf0f2 - fix: 处理 reject/close 上的 NotAllowedError (#4246)
v10.0.0-beta.2 on 3/28/2023
0823d - fix: 显示 Firefox 的设备 (#2798)

在 MIT 许可证下发布。