muaz-khan / RecordRTC

RecordRTC is WebRTC JavaScript library for audio/video as well as screen activity recording. It supports Chrome, Firefox, Opera, Android, and Microsoft Edge. Platforms: Linux, Mac and Windows.
https://www.webrtc-experiment.com/RecordRTC/
MIT License
6.53k stars 1.75k forks source link

How to end share dialog with getDisplayMedia? #583

Closed methodbox closed 4 years ago

methodbox commented 4 years ago

I'm trying to programmatically end the "share" dialog ("Stop Sharing" button) when I end a recording.

I've seen it working here: https://webrtc.github.io/samples/src/content/getusermedia/getdisplaymedia/

But when I call your function recording.stopRecording it's not ending the shared screen. I'm assuming there's an event that is fired when this happens, or an event I can trigger to end the shared screen.

methodbox commented 4 years ago

You also have a working example of this here: https://www.webrtc-experiment.com/RecordRTC/

Seeing if I can decipher what your code is doing to close that dialog. It doesn't look like there's anything in your API to control this.

methodbox commented 4 years ago

I found a solution from an older Stackoverflow post here: https://stackoverflow.com/questions/46241744/how-to-get-the-event-handler-for-stop-sharing-button-in-chrome-browser-in-emberj

You can add a handler to your video track so that when the sharing is ended using the native API UI “Stop sharing” button you catch that event - you should probably end the recording programmatically, too, if you use this method as in my experience the recording ends up with time code issues if you don’t; i.e it only stops because it fails and that results in corrupted videos.

Using the example below worked for me; likely your video track will be at index 0 unless you have more than one; generally your sharing instance is at 0.

YourMediaStream.getVideoTracks()[0].addEventListener(‘ended’, () => /** any method you want to fire when the screen sharing is stopped */)
dcerisano commented 1 year ago

Window.reload() will cancel all user initiated web APIs. You can cleanly exit your sharing code in onLoad() or onUnload() events (which you should be doing anyway). Because security reasons.