javascriptwebscreensharing

web browser screen share - "stop share" handler


Once a screen share is started by "navigator.mediaDevices.getDisplayMedia()" and confirmed by user, there is a posibility in each browser to stop a screen share (for example Chrome - see pic). enter image description here

How to attach a javascript handler on the event, when a screen share is stopped by this browser button?


Solution

  • Have a look at this sample here: https://github.com/webrtc/samples/blob/gh-pages/src/content/getusermedia/getdisplaymedia/js/main.js#L31

    on line 31 an event handler is attached to MediaStreamTrack

    stream.getVideoTracks()[0].addEventListener('ended', () => {
        console.log('The user has ended sharing the screen');
    });