Twilio房间无法断开连接/网络摄像头LED保持点亮

时间:2018-08-21 11:09:06

标签: javascript twilio

我正在将twilio视频实现到我的js应用程序中。我想要的行为是,如果远程参与者从会议室断开连接,则本地参与者也应该离开会议室,并且网络摄像头应该停用。

但是,发生的情况是参与者离开并且网络摄像头LED仍然亮着,并且似乎仍然流向twilio,直到我关闭浏览器。

我将如何解决这个问题?

   function connectVideo(accessToken){

            const Video = Twilio.Video;

            Video.connect(accessToken, { name: customerToken}).then(room => {
                console.log('Connected to Room "%s"', room.name);
                inCall = 1

                room.participants.forEach(participantConnected);
                room.on('participantConnected', participantConnected);
                room.once('participantDisconnected', participant => {
                    console.log(`Participant "${participant.identity}" has disconnected from the Room!`);
                    room.disconnect();
                   var div = document.getElementById(participant.sid);
                    div.remove()
                    inCall = 0
                });

                room.on('disconnected', room => {
                    // Detach the local media elements
                    room.localParticipant.tracks.forEach(publication => {
                        const attachedElements = publication.track.detach();
                        console.log("unsubscribed from: " + publication.track)
                        attachedElements.forEach(element => element.remove());
                    });
                });
            });

            function participantConnected(participant) {
                console.log('Participant "%s" connected', participant.identity);
                const div = document.createElement('div');
                div.id = participant.sid;
                participant.on('trackSubscribed', track => trackSubscribed(div, track));
                participant.on('trackUnsubscribed', trackUnsubscribed);
                participant.tracks.forEach(publication => {
                    if (publication.isSubscribed) {
                        trackSubscribed(div, publication.track);
                    }
                });
                document.body.appendChild(div);
            }
            function trackSubscribed(div, track) {
                div.appendChild(track.attach());
            }

            function trackUnsubscribed(track) {
                track.detach().forEach(element => element.remove());
            }
        }

1 个答案:

答案 0 :(得分:1)

这里是Twilio开发人员的传播者。

当您的参与者从会议室断开连接时,您当前正在分离轨道。附加和分离仅指显示和删除DOM中的视频和音频元素。要完全停止媒体播放,从而熄灭相机LED,您还需要在每个音轨上调用stop()

               room.localParticipant.tracks.forEach(publication => {
                    publication.track.stop();
                    const attachedElements = publication.track.detach();
                    console.log("unsubscribed from: " + publication.track)
                    attachedElements.forEach(element => element.remove());
                });