| ¶Ô±ÈÐÂÎļþ |
| | |
| | | import { type VNodeRef, watch } from 'vue' |
| | | |
| | | /** |
| | | * æååºå¼ MediaStream æå° `<video>` / `<audio>` å
ç´ ç srcObject ä¸ï¼ |
| | | * stream ä¸ºç©ºæ¶æ¸
æ srcObjectï¼é¿å
设å¤å
³éåç»é¢å¡å¨æåä¸å¸§ |
| | | */ |
| | | export function useMediaStreamElement<T extends HTMLMediaElement>( |
| | | streamSource: () => MediaStream | null | undefined |
| | | ): VNodeRef { |
| | | let el: T | null = null |
| | | let currentStream: MediaStream | null | undefined |
| | | |
| | | const syncStream = () => { |
| | | if (el) { |
| | | el.srcObject = currentStream || null |
| | | } |
| | | } |
| | | |
| | | watch( |
| | | streamSource, |
| | | (stream) => { |
| | | currentStream = stream |
| | | syncStream() |
| | | }, |
| | | { flush: 'post', immediate: true } |
| | | ) |
| | | |
| | | return (value) => { |
| | | if (value instanceof HTMLMediaElement) { |
| | | el = value as T |
| | | syncStream() |
| | | return |
| | | } |
| | | |
| | | if (el) { |
| | | el.srcObject = null |
| | | } |
| | | el = null |
| | | } |
| | | } |