Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useUserMedia.md
1---2category: Sensors3related: useDevicesList, usePermission4---56# useUserMedia78Reactive [`mediaDevices.getUserMedia`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia) streaming.910## Usage1112```vue13<script setup lang="ts">14import { useUserMedia } from '@vueuse/core'15import { useTemplateRef, watchEffect } from 'vue'1617const { stream, start } = useUserMedia()18start()1920const videoRef = useTemplateRef('video')21watchEffect(() => {22// preview on a video element23videoRef.value.srcObject = stream.value24})25</script>2627<template>28<video ref="video" />29</template>30```3132### Devices3334```ts35import { useDevicesList, useUserMedia } from '@vueuse/core'36import { computed, reactive } from 'vue'3738const {39videoInputs: cameras,40audioInputs: microphones,41} = useDevicesList({42requestPermissions: true,43})44const currentCamera = computed(() => cameras.value[0]?.deviceId)45const currentMicrophone = computed(() => microphones.value[0]?.deviceId)4647const { stream } = useUserMedia({48constraints: reactive({49video: { deviceId: currentCamera },50audio: { deviceId: currentMicrophone, }51})52})53```5455## Type Declarations5657```ts58export interface UseUserMediaOptions extends ConfigurableNavigator {59/**60* If the stream is enabled61* @default false62*/63enabled?: MaybeRef<boolean>64/**65* Recreate stream when deviceIds or constraints changed66*67* @default true68*/69autoSwitch?: MaybeRef<boolean>70/**71* MediaStreamConstraints to be applied to the requested MediaStream72* If provided, the constraints will override videoDeviceId and audioDeviceId73*74* @default {}75*/76constraints?: MaybeRef<MediaStreamConstraints>77}78export interface UseUserMediaReturn extends Supportable {79stream: Ref<MediaStream | undefined>80start: () => Promise<MediaStream | undefined>81stop: () => void82restart: () => Promise<MediaStream | undefined>83constraints: Ref<MediaStreamConstraints | undefined>84enabled: ShallowRef<boolean>85autoSwitch: ShallowRef<boolean>86}87/**88* Reactive `mediaDevices.getUserMedia` streaming89*90* @see https://vueuse.org/useUserMedia91* @param options92*/93export declare function useUserMedia(94options?: UseUserMediaOptions,95): UseUserMediaReturn96```97