简体   繁体   English

如何使用 ReactJs 将网络摄像头流式传输到移动浏览器?

[英]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>
    );
  }
}

And this is the result :这是结果:
在此处输入图片说明

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.

相关问题 如何使用webrtc将摄像头流发送到wowzaurl - How to send webcam stream to wowzaurl using webrtc ReactJS-来自网络摄像头的视频流不起作用 - ReactJS - video stream from webcam not working 如何使用 react js 组件在网页上流式传输网络摄像头提要? - How to stream webcam feed on web page using react js component? 如何将服务器网络摄像头流式传输到servlet - How to stream server webcam to servlet 如何在HTML 5中将网络摄像头流式传输到客户端 - How to stream webcam to client in HTML 5 用于移动浏览器的 Reactjs 中的事件侦听器 - Event Listener in Reactjs for mobile browser ReactJS:如何确定应用程序是在移动浏览器还是桌面浏览器上查看 - ReactJS: How to determine if the application is being viewed on mobile or desktop browser 如何在 reactjs 的视频网络摄像头上添加边框? - How add a border frame on a video webcam in reactjs? 如何使用 ReactJS 在浏览器中链接下载为 PDF - How to link for download as PDF in browser using ReactJS 在使用网络摄像头输入进行面部识别(opencv4nodejs)时,如何在服务器(javascript)上记录和下载/上传网络摄像头 stream? - How do I record AND download / upload the webcam stream on server (javascript) WHILE using the webcam input for facial recognition (opencv4nodejs)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM