I have a react component which starts a webcam when the component VideoPage is mounted
However, when I go to another page, the webcam is still running in the background cause my laptop's webcam lights is still on
I believe I will need to 'kill' the webcam in React lifecycle method ComponentWillUnmount when exiting the component. How am I able to do that?
Below are my codes... Appreciate advise. Thanks
import React from "react";
export default class VideoPage extends React.Component {
constructor(props) {
super(props);
this.videoTag = React.createRef();
this.state = {
loading: false
};
}
componentDidMount() {
navigator.mediaDevices
.getUserMedia({ video: true })
.then(this.handleStream)
.catch(err => console.log(err));
}
handleStream = async stream => {
// start receiving stream from webcam
this.videoTag.current.srcObject = stream;
console.log("The video is ready");
};
render() {
return (
<div>
<div style={{ paddingTop: 20 }}>
<video
id="myvideo"
ref={this.videoTag}
width={426}
height={240}
autoPlay
title="video"
/>
</div>
</div>
);
}
}