如何在没有焦点的情况下使用getDisplayMedia全屏共享屏幕?

问题描述 投票:0回答:1

我正在使用 webrtc 和 getDisplayMedia 实现屏幕共享服务。您能否解释一下,当选项卡无疑会失去焦点时,这是如何实现的?我正在尝试共享某些应用程序或整个屏幕本身的显示。

似乎除非我共享创建屏幕共享的选项卡,否则屏幕共享总是会冻结流。

我在 Windows 11 上使用 chrome 117.0.5938.152

javascript html5-canvas webrtc
1个回答
0
投票

当我在 Edge 上取消焦点选项卡时,此代码不会停止共享:

<!DOCTYPE html>
<html>
<head>
    <title>Screen Sharing Example</title>
</head>
<body>
    <h1>Screen Sharing Example</h1>
    <button id="startButton">Start Screen Sharing</button>
    <video id="screenShare" autoplay controls></video>
    
    <script>
        const startButton = document.getElementById('startButton');
        const screenShareVideo = document.getElementById('screenShare');

        startButton.addEventListener('click', async () => {
            try {
                const stream = await navigator.mediaDevices.getDisplayMedia({ video: { mediaSource: 'screen' } });
                screenShareVideo.srcObject = stream;
            } catch (error) {
                console.error('Error accessing screen sharing:', error);
            }
        });
    </script>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.