简体   繁体   English

如何使用 WebRTC 在 React JSX 中启动和停止网络摄像头 stream 和麦克风?

[英]How to start and stop both the webcam stream and microphone in React JSX using WebRTC?

I already have getUserMedia working in my ReactJSX file.我已经在我的 ReactJSX 文件中使用了 getUserMedia。 I'm new to ReactJSX, and i've viewed some stackoverflow posts related to this, but unfortunately many of the existing instructions include a line of code/function that is deprecated by webRTC.我是 ReactJSX 的新手,我已经查看了一些与此相关的 stackoverflow 帖子,但不幸的是,许多现有指令包含一行代码/函数,已被 webRTC 弃用。

However, I haven't been able to figure how to stop the web cam stream and microphone in React JSX , in particular.但是,我无法弄清楚如何停止 web 凸轮 stream 和React JSX中的麦克风,特别是。 And since this language is quite new for me, can anyone help me?而且由于这种语言对我来说很新,任何人都可以帮助我吗?

This is the working code I have before the return() tag in React JSX这是我在 React JSX 中的return()标记之前的工作代码

  navigator.mediaDevices.getUserMedia({
      audio: true,
      video: true
    })
    .then(stream => {
      document.getElementById("startstream").srcObject = stream;
    })

And this is the working code I have to display and enable the camera and microphone after the return() tag in React JSX:这是我必须在 React JSX 中的return()标记之后显示和启用摄像头和麦克风的工作代码:

<video id="startstream" autoPlay />

Both of those code snippets work, and it allows the user to start the stream/start their microphone, but how do I start and stop the webcam/microphone using a button in ReactJSX?这两个代码片段都有效,它允许用户启动流/启动他们的麦克风,但是如何使用 ReactJSX 中的按钮启动停止网络摄像头/麦克风? I've seen something similar online using another coding language, but if anyone code help me to get it to work with ReactJSX, that would be appreciated.我在网上看到过类似的使用另一种编码语言的东西,但如果有人代码帮助我让它与 ReactJSX 一起工作,那将不胜感激。

import React from "react";
import "./styles.css";

class WebRTCSample extends React.Component {
  constructor(props) {
    super(props);
    this.state = { localStream: null };
  }

  componentDidMount() {}

  startWebCam = () => {
    const that = this;
    navigator.mediaDevices
      .getUserMedia({
        audio: true,
        video: true
      })
      .then((stream) => {
        that.setState({ localStream: stream });
      });
  };

  stopWebCam = () => {
    this.state.localStream.getTracks().forEach((track) => {
      track.stop();
    });
  };

  render() {
    return (
      <div className="App">
        {/* <h1>Hello GetUserMedia</h1> */}
        {this.state.localStream && (
          <video
            autoPlay
            ref={(video) => {
              if (video) {
                video.srcObject = this.state.localStream;
              }
            }}
            // src={this.state.localStream}
          />
        )}
        <div className="startStopWebCam">
          <button
            className="WebCamButton"
            onClick={this.startWebCam.bind(this)}
          >
            Start
          </button>
          <button className="WebCamButton" onClick={this.stopWebCam.bind(this)}>
            Stop
          </button>
        </div>
      </div>
    );
  }
}

export default WebRTCSample;

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM