📅  最后修改于: 2023-12-03 15:15:33.639000             🧑  作者: Mango
The MediaStream.active property is a read-only property of the MediaStream object in the HTML DOM. It returns a Boolean value indicating whether or not the MediaStream is active, i.e. whether or not it's currently rendering media.
let isActive = mediaStream.active;
let videoStream = await navigator.mediaDevices.getUserMedia({ video: true });
console.log(videoStream.active); // true
videoStream.getTracks().forEach(track => {
track.stop();
});
console.log(videoStream.active); // false
In this example, we create a MediaStream object called videoStream
using the getUserMedia
method. We then check the active
property of the videoStream
object, which returns true
since the stream is currently rendering media. We then stop all the tracks in the stream using the stop
method of each track object, and check the active
property again, which now returns false
.
active
property reflects the current state of the MediaStream object, and can change as tracks are added or removed from the stream, or if the user stops or starts rendering media.active
property can be useful to determine when to end a session or perform other cleanup tasks related to the MediaStream object.In this article, we learned about the MediaStream.active property in the HTML DOM. We covered its syntax, return value, and provided some examples to illustrate its usage. We also noted some important points to keep in mind when working with this property.