vueuse 中文文档

VueUse useDisplayMedia 共享屏幕

VueUse useDisplayMedia 可以获取你指定的屏幕或应用并进行录屏或者共享屏幕。

<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { useDisplayMedia } from '@vueuse/core'

const video = ref<HTMLVideoElement>()
const { stream, enabled } = useDisplayMedia()

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' }} sharing my screen
      </button>
    </div>


    <div>
      <video
        ref="video"
        muted
        autoplay
        controls
        class="h-100 w-auto"
      />
    </div>
  </div>
</template>

类型定义

//参数
export interface UseDisplayMediaOptions extends ConfigurableNavigator {
  /**
   * If the stream is enabled
   * @default false
   */
  enabled?: MaybeRef<boolean>
  /**
   * If the stream video media constraints
   */
  video?: boolean | MediaTrackConstraints | undefined
  /**
   * If the stream audio media constraints
   */
  audio?: boolean | MediaTrackConstraints | undefined
}
/**
 * Reactive `mediaDevices.getDisplayMedia` streaming
 *
 * @see https://vueuse.org/useDisplayMedia
 * @param options
 */
export declare function useDisplayMedia(options?: UseDisplayMediaOptions): {
  isSupported: ComputedRef<boolean>
  stream: Ref<MediaStream | undefined>
  start: () => Promise<MediaStream | undefined>
  stop: () => void
  enabled: Ref<boolean>
}
export type UseDisplayMediaReturn = ReturnType<typeof useDisplayMedia>
Catalog
vueuse 入门 State 状态 Elements 元素 Browser 浏览器 Sensors 感应监测 onClickOutside 侦听元素外部点击 onKeyStroke 监听按键 onLongPress 长按事件 onStartTyping 非编辑元素输入时触发 useBattery 获取电池状态。 useDeviceMotion 设备方向及运动信息 useDeviceOrientation 方向变化信息 useDevicePixelRatio 屏幕像素比 useDevicesList 可用设备列表