我的相机有点怪。当我拍照时,它会立即关闭。但是,当我的组件卸载时,即使调用了完全相同的函数,在我的相机关闭之前可能需要10秒钟。我想它还是开着的,因为“照像机灯”还是开着的。以下是背后的逻辑:
class Webcam extends React.Component<Props> {
...
videoRef: any = React.createRef();
componentWillUnmount() {
this.destroyCam();
}
destroyCam() {
this.videoRef.srcObject.getTracks().forEach((track: MediaStream) => {
track.stop();
});
}
onShoot = () => {
var timer = setInterval(() => {
this.countdown--;
if (this.countdown === 0) {
this.destroyCam();
clearInterval(timer);
}
}, 1000);
};
render() {
return(
...
<video ref={el => (this.videoRef = el)}/>
)
}
}
就像我说的,当onShoot启动时,它会立即关闭摄像头。为什么在卸载组件时它不启动?