김 양의 멋따라 개발따기
React - getDisplayMedia()로 화면공유하기 본문
1. 문제상황
google-meet을 클론하던 중 화면 공유에서 막힘
2. getDisplayMedia()로 해결!
import React,{useRef} from "react"
const Stream = () => {
const video_ref = useRef();
//화면공유
const onClickScreenBtn = async () => {
let captureStream;
try {
captureStream = await navigator.mediaDevices.getDisplayMedia();
} catch (err) {
console.log(err);
}
return captureStream;
};
return (
<div>
<video controls ref={video_ref}></video>
<button onClick={onClickScreenBtn}>screen</button>
</div>
);
};
export default Stream;
결과
버튼을 눌렀을 때 화면공유 팝업 등장!
출처 : https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getDisplayMedia
MediaDevices.getDisplayMedia() - Web APIs | MDN
The MediaDevices interface's getDisplayMedia() method prompts the user to select and grant permission to capture the contents of a display or portion thereof (such as a window) as a MediaStream.
developer.mozilla.org
'React!' 카테고리의 다른 글
React- 배경음악 재생하고 화면 벗어나면 음악 중지하기 (0) | 2023.08.09 |
---|---|
react - drag 기능 구현하기 (0) | 2023.04.11 |
React로 언어 변환하기(react-i18next) (1) | 2023.01.19 |
React - 라이프사이클 메서드의 이해 (0) | 2022.12.15 |
React의 짝꿍 Redux 뽀개기 (0) | 2022.12.13 |