【发布时间】:2020-08-31 23:31:51
【问题描述】:
我有一个 nodeJS 项目,我在 System 和 Mobile 上都使用它。
我需要执行以下步骤 - 1.增加一个按钮来打开/关闭手电筒。 2. 仅当该功能支持时才应显示该按钮 手机和浏览器 3.灯应该默认关闭
当我使用下面提到的代码时,它会从我的系统启用我的网络摄像头闪光灯,但它无法在我的手机上运行。
闪光灯开/关
//Test browser support
const SUPPORTS_MEDIA_DEVICES = 'mediaDevices' in navigator;
if (SUPPORTS_MEDIA_DEVICES) {
//Get the environment camera (usually the second one)
navigator.mediaDevices.enumerateDevices().then(devices => {
const cameras = devices.filter((device) => device.kind === 'videoinput');
if (cameras.length === 0) {
throw 'No camera found on this device.';
}
const camera = cameras[cameras.length - 1];
// Create stream and get video track
navigator.mediaDevices.getUserMedia({
video: true
}).then(stream => {
const track = stream.getVideoTracks()[0];
track.applyConstraints({
advanced: [{torch: false}]
});
//Create image capture object and get camera capabilities
const imageCapture = new ImageCapture(track)
const photoCapabilities = imageCapture.getPhotoCapabilities().then(() => {
//todo: check if camera has a torch
//let there be light!
const btn = document.querySelector('.switch');
btn.addEventListener('click', function(){
isTorchOn = !isTorchOn;
track.applyConstraints({
advanced: [{torch: isTorchOn}]
});
});
});
});
});
//The light will be on as long the track exists
}
谁能提出解决方案?
【问题讨论】:
-
如果您的代码需要在每个移动设备上运行,我认为这是无法实现的。据我所知,只有在 Chrome 上才能通过浏览器访问相机控件。
-
是的,Chrome 可以为我工作。我正在通过手机中的 chrome 访问它。
标签: node.js nodejs-stream mediastream