Skip to content

useUserMedia

Category
Export Size
658 B
Last Changed
last month
Related

Reactive mediaDevices.getUserMedia streaming.

Demo

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

Show 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

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Vida Xie
Anthony Fu
bab
SerKo
OrbisK
Waleed Khaled
Martin
Jelf
wheat
Shinigami
Alex Kozack

Changelog

v14.0.0-beta.1 on
4b3e9 - fix: add deep watch to constraints (#5046)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.8.0 on
6d44d - fix: stop stream on scope dispose (#3757)

Released under the MIT License.

The new era of AI engineering.
Workflows, artifacts, and judgment that scale.
Request Early Access