useDevicesList
响应式 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 }">
摄像头:{{ videoInputs }}
麦克风:{{ audioInputs }}
扬声器:{{ audioOutputs }}
</UseDevicesList>
</template>
类型声明
typescript
export interface UseDevicesListOptions extends ConfigurableNavigator {
onUpdated?: (devices: MediaDeviceInfo[]) => void
/**
* 如果权限未授予,则立即请求权限,否则标签和设备 ID 可能为空
*
* @default false
*/
requestPermissions?: boolean
/**
* 请求媒体权限类型
*
* @default { audio: true, video: true }
*/
constraints?: MediaStreamConstraints
}
export interface UseDevicesListReturn {
/**
* 所有设备
*/
devices: Ref<MediaDeviceInfo[]>
videoInputs: ComputedRef<MediaDeviceInfo[]>
audioInputs: ComputedRef<MediaDeviceInfo[]>
audioOutputs: ComputedRef<MediaDeviceInfo[]>
permissionGranted: Ref<boolean>
ensurePermissions: () => Promise<boolean>
isSupported: ComputedRef<boolean>
}
/**
* 响应式 `enumerateDevices` 列出可用的输入/输出设备
*
* @see https://vueuse.org/useDevicesList
* @param options
*/
export declare function useDevicesList(
options?: UseDevicesListOptions,
): UseDevicesListReturn
源码
贡献者
Anthony Fu
一纸忘忧
wheat
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.4.0
on 1/10/2025v12.0.0-beta.1
on 11/21/2024v11.2.0
on 10/30/2024v10.0.0-beta.2
on 3/28/2023v9.11.0
on 1/17/2023