/
demo.vue
48 lines (43 loc) · 1.2 KB
/
demo.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { useDevicesList, useUserMedia } from '@vueuse/core'
const currentCamera = ref<string>()
const { videoInputs: cameras } = useDevicesList({
requestPermissions: true,
onUpdated() {
if (!cameras.value.find(i => i.deviceId === currentCamera.value))
currentCamera.value = cameras.value[0]?.deviceId
},
})
const video = ref<HTMLVideoElement>()
const { stream, enabled } = useUserMedia({
constraints: { video: { deviceId: currentCamera } },
})
watchEffect(() => {
if (video.value)
video.value.srcObject = stream.value!
})
</script>
<template>
<div class="flex flex-col gap-4 text-center">
<div>
<button @click="enabled = !enabled">
{{ enabled ? 'Stop' : 'Start' }}
</button>
</div>
<div>
<div
v-for="camera of cameras"
:key="camera.deviceId"
class="px-2 py-1 cursor-pointer"
:class="{ 'text-primary': currentCamera === camera.deviceId }"
@click="currentCamera = camera.deviceId"
>
{{ camera.label }}
</div>
</div>
<div>
<video ref="video" muted autoplay controls class="h-100 w-auto" />
</div>
</div>
</template>