I'd like to be informed about a MediaStreamTrack
's end. According to MDN an ended
event is
Sent when playback of the track ends (when the value
readyState
changes toended
). Also available using theonended
event handler property.
So I should be able to setup my callback(s) like:
const [track] = stream.getVideoTracks();
track.addEventListener('ended', () => console.log('track ended'));
track.onended = () => console.log('track onended');
and I expect those to be invoked, once I stop the track via:
tracks.forEach(track => track.stop());
// for good measure? See
// https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/stop#Stopping_a_video_stream
videoElem.srcObject = null;
The problem I'm having is that the callbacks are not invoked. I built the following JSFiddle, where 3 MediaStreams are created in 3 different ways:
getUserMedia
getDisplayMedia
getCaptureStream
(canvas element)
I also have 3 buttons which stop all tracks for the respective MediaStream
. The behaviour is as follows:
- All 3 streams are
inactive
, the MediaStream'soninactive
callback is triggered (in Chrome, seems like Firefox doesn't support this). - All tracks have a
readyState
ofended
after being stopped. - If I stop the screen stream (2. getDisplayMedia) via Chrome's UI, the track ended callback(s) are invoked.
I know that you have to watch out for the track being used by multiple sources, but that shouldn't be the case here, right? Am I missing something obvious?
Since multiple tracks may use the same source (for example, if two tabs are using the device's microphone), the source itself isn't necessarily immediately stopped. It is instead disassociated from the track and the track object is stopped. Once no media tracks are using the source, the source may actually be completely stopped.