[英]How to stream webcam into mobile browser by using ReactJs?
I have created a simple react app that streams the webcam video stream on the browser.我创建了一个简单的 React 应用程序,可以在浏览器上流式传输网络摄像头视频流。 Here's the link to the github project : Basic WebCam Streamer
这是github项目的链接: Basic WebCam Streamer
The code is pretty simple and straightforward :代码非常简单明了:
class AppStreamCam extends React.Component {
constructor(props) {
super(props);
this.streamCamVideo= this.streamCamVideo.bind(this)
}
streamCamVideo() {
var constraints = { audio: true, video: { width: 1280, height: 720 } };
navigator.mediaDevices
.getUserMedia(constraints)
.then(function(mediaStream) {
var video = document.querySelector("video");
video.srcObject = mediaStream;
video.onloadedmetadata = function(e) {
video.play();
};
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
}); // always check for errors at the end.
}
render() {
return (
<div>
<div id="container">
<video autoPlay={true} id="videoElement" controls></video>
</div>
<br/>
<button onClick={this.streamCamVideo}>Start streaming</button>
</div>
);
}
}
Once, I click on the button, the webcam turns on and starts streaming into the browser.有一次,我单击按钮,网络摄像头打开并开始流式传输到浏览器。
Here's my problem :这是我的问题:
When I open chrome on my phone and enter the localServer address, and click on the button, the app crashes since obviously the app code is meant to be run from the pc browser so that it may turn the pc webcam.当我在手机上打开 chrome 并输入 localServer 地址,然后单击按钮时,应用程序崩溃了,因为显然应用程序代码是从 PC 浏览器运行的,以便它可以打开 PC 网络摄像头。
So when I click on the button from my phone, I understandably get this error:所以当我点击手机上的按钮时,我可以理解地得到这个错误:
TypeError: Cannot read property 'getUserMedia' of undefined
类型错误:无法读取未定义的属性“getUserMedia”
My goal is to click on the button from my mobile browser and start streaming the pc webcam on my mobile browser just like on the pc.我的目标是单击我的移动浏览器中的按钮并开始在我的移动浏览器上流式传输 PC 网络摄像头,就像在 PC 上一样。
However, I do not know from where to start exactly.但是,我不知道从哪里开始。 Any help?
有什么帮助吗?
I have solved this issue.我已经解决了这个问题。
1. Open package.json and paste this inside scripts : 1. 打开package.json并将其粘贴到脚本中:
"start": "set HTTPS=true&&react-scripts start"
"start": "设置 HTTPS=true&&react-scripts 开始"
This should serve the app over https这应该通过 https 为应用程序提供服务
2. If this gives you this error: 2. 如果这给你这个错误:
React app error: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS
React 应用程序错误:无法构建“WebSocket”:可能无法从通过 HTTPS 加载的页面启动不安全的 WebSocket 连接
Open打开
node_modules/react-dev-utils/webpackHotDevClient.js
node_modules/react-dev-utils/webpackHotDevClient.js
And paste this code inside the definition of the connection :并将此代码粘贴到连接的定义中:
protocol: window.location.protocol === 'https:' ? 'wss' : 'ws',
This is apparently a bug in react-sripts that hasn't been solved yet.这显然是react-sript 中的一个尚未解决的错误。 If https protocol is being used we should use WebSockets over SSL/TLS (WSS) protocol instead of WebSockets (WS).
如果使用 https 协议,我们应该使用 WebSockets over SSL/TLS (WSS) 协议而不是 WebSockets (WS)。 You can learn more about it here :
您可以在此处了解更多信息:
NOTE: This will not stream your pc webcam into your phone but rather the phone's camera.注意:这不会将您的电脑网络摄像头传输到您的手机中,而是将手机的摄像头传输到您的手机中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.