agora 屏幕共享和视频播放切换错误

Screen sharing and video broadcasting switching error in agora

所以,我一直在做一个使用agora的web sdk的项目。我基本上想在屏幕共享和视频广播之间来回无缝切换,在这两种情况下都共享音频。

这是我编写的代码:

const localPlay = {
    screen: {
        playing: false,
        initialised: false
    },
    video: {
        playing: false,
        initialised: false
    }
};


// Initialise two clients and two streams, with the following stream specs
 const screenStreamSpec = {
                streamID: uid,
                audio: false,
                video: false,
                screen: true,
            };

const videoStreamSpec = {
                streamID: uid,
                audio: true,
                video: true,
                screen: false,
            };

// ...

document.getElementById("Screen-share").addEventListener("click", function() {

    if(!localPlay.screen.initialised) {
        return
    } else {
        if(!localPlay.screen.playing && !localPlay.video.playing) {
            screenStream.play("Screen");
            rtc.client.screenClient.publish(screenStream);
            localPlay.screen.playing = true;                        
            return;
        }

        if(localPlay.screen.playing) {
            return;
        }

        videoStream.stop();
        localPlay.screen.playing = false;
        screenStream.play("Screen");
        localPlay.video.playing = true;

        let newTrack = videoStream.getVideoTrack();
        screenStream.replaceTrack(newTrack);
    }
});


document.getElementById("Video-share").addEventListener("click", function() {

    if(!localPlay.video.initialised) {
        return
    } else {
        if(!localPlay.video.playing && !localPlay.screen.playing) {
            videoStream.play("Screen");
            rtc.client.videoClient.publish(videoStream);
            localPlay.video.playing = true;
            return;
        }

        if(localPlay.video.playing) {
            return;
        }

        screenStream.stop();
        localPlay.video.playing = false;
        videoStream.play("Screen");
        localPlay.screen.playing = true;

        let newTrack = screenStream.getVideoTrack();
        videoStream.replaceTrack(newTrack);
    }
});

代码按照this(方案3)。

错误是我第一次尝试做任何一个时,它们都起作用了。但是,假设我从屏幕共享开始,然后切换到视频广播,摄像头关闭,屏幕共享关闭,我正在共享的屏幕旋转并显示在 Screen div 中。如果我再尝试切换一次,div 就会完全变黑。

我哪里做错了?

您真的不应该使用替换曲目,因为并非所有浏览器都支持它,而且它是一项相对较新的功能。

为了更稳定的解决方案,我建议使用 2 个客户端,一个用于摄像头,另一个用于屏幕共享。我这里有一个例子:https://github.com/digitallysavvy/group-video-chat

如果您想在两者之间快速切换,可以使用 muteVideo 事件在流之间切换。