Add comprehensive reference documentation for: - Vitest: environments, projects/workspaces, type testing, vi utilities - Pinia: HMR, Nuxt integration, SSR setup - Vue: built-in components (Transition, Teleport, Suspense, KeepAlive) and advanced directives
97 lines
2.2 KiB
Markdown
97 lines
2.2 KiB
Markdown
---
|
|
category: Sensors
|
|
related: useDevicesList, usePermission
|
|
---
|
|
|
|
# useUserMedia
|
|
|
|
Reactive [`mediaDevices.getUserMedia`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia) streaming.
|
|
|
|
## Usage
|
|
|
|
```vue
|
|
<script setup lang="ts">
|
|
import { useUserMedia } from '@vueuse/core'
|
|
import { useTemplateRef, watchEffect } from 'vue'
|
|
|
|
const { stream, start } = useUserMedia()
|
|
start()
|
|
|
|
const videoRef = useTemplateRef('video')
|
|
watchEffect(() => {
|
|
// preview on a video element
|
|
videoRef.value.srcObject = stream.value
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<video ref="video" />
|
|
</template>
|
|
```
|
|
|
|
### Devices
|
|
|
|
```ts
|
|
import { useDevicesList, useUserMedia } from '@vueuse/core'
|
|
import { computed, reactive } from 'vue'
|
|
|
|
const {
|
|
videoInputs: cameras,
|
|
audioInputs: microphones,
|
|
} = useDevicesList({
|
|
requestPermissions: true,
|
|
})
|
|
const currentCamera = computed(() => cameras.value[0]?.deviceId)
|
|
const currentMicrophone = computed(() => microphones.value[0]?.deviceId)
|
|
|
|
const { stream } = useUserMedia({
|
|
constraints: reactive({
|
|
video: { deviceId: currentCamera },
|
|
audio: { deviceId: currentMicrophone, }
|
|
})
|
|
})
|
|
```
|
|
|
|
## Type Declarations
|
|
|
|
```ts
|
|
export interface UseUserMediaOptions extends ConfigurableNavigator {
|
|
/**
|
|
* If the stream is enabled
|
|
* @default false
|
|
*/
|
|
enabled?: MaybeRef<boolean>
|
|
/**
|
|
* Recreate stream when deviceIds or constraints changed
|
|
*
|
|
* @default true
|
|
*/
|
|
autoSwitch?: MaybeRef<boolean>
|
|
/**
|
|
* MediaStreamConstraints to be applied to the requested MediaStream
|
|
* If provided, the constraints will override videoDeviceId and audioDeviceId
|
|
*
|
|
* @default {}
|
|
*/
|
|
constraints?: MaybeRef<MediaStreamConstraints>
|
|
}
|
|
export interface UseUserMediaReturn extends Supportable {
|
|
stream: Ref<MediaStream | undefined>
|
|
start: () => Promise<MediaStream | undefined>
|
|
stop: () => void
|
|
restart: () => Promise<MediaStream | undefined>
|
|
constraints: Ref<MediaStreamConstraints | undefined>
|
|
enabled: ShallowRef<boolean>
|
|
autoSwitch: ShallowRef<boolean>
|
|
}
|
|
/**
|
|
* Reactive `mediaDevices.getUserMedia` streaming
|
|
*
|
|
* @see https://vueuse.org/useUserMedia
|
|
* @param options
|
|
*/
|
|
export declare function useUserMedia(
|
|
options?: UseUserMediaOptions,
|
|
): UseUserMediaReturn
|
|
```
|