[英]JavaScript programatically check if camera is being used
I'm writing functional tests for a video chat app.我正在为视频聊天应用程序编写功能测试。
I want to make sure that when the user leaves the meeting the camera turns off.我想确保当用户离开会议时摄像头关闭。 So, I'm trying to check if the camera is being used or not.
所以,我试图检查是否正在使用相机。
Is there a way to do that programatically?有没有办法以编程方式做到这一点? I couldn't find any methods on
navigator.MediaDevices
that say "hey your camera is being used".我在
navigator.MediaDevices
上找不到任何说“嘿,你的相机正在被使用”的方法。
Here is how I solved it in TestCafe by "spying" on getUserMedia
:这是我在 TestCafe 中通过“监视”
getUserMedia
解决它的方法:
const overWriteGetUserMedia = ClientFunction(() => {
const realGetUserMedia = navigator.mediaDevices.getUserMedia;
const allRequestedTracks = [];
navigator.mediaDevices.getUserMedia = constraints =>
realGetUserMedia(constraints).then(stream => {
stream.getTracks().forEach(track => {
allRequestedTracks.push(track);
});
return stream;
});
return allRequestedTracks;
});
test('leaving a meeting should end streams', async t => {
const allRequestedTracks = await overWriteGetUserMedia();
await t.wait(5000); // wait for streams to start;
await t.click(screen.getByLabelText(/leave/i));
await t.click(screen.getByLabelText(/yes, leave the meeting/i));
await t.wait(1000); // wait for navigation;
const actual = !allRequestedTracks.some(track => !track.ended);
const expected = true;
await t.expect(actual).eql(expected);
});
You can use navigator.mediaDevices.getUserMedia
method to get access of user camera, and user active
value to check is camera already activated.您可以使用
navigator.mediaDevices.getUserMedia
方法来获取用户摄像头的访问权限,并检查用户active
值是否已激活摄像头。
If user block the permission to the camera you will receive an error.如果用户阻止对相机的权限,您将收到错误消息。
Hope this will be work for you.希望这对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.