[英]I see an empty page and this message in inspector " This file is a template and if you open it directy in the browser you will see an empty page."
I am new to reactjs and I am trying to display a local html-video inside of the react Application.我是 reactjs 的新手,我正在尝试在 react 应用程序中显示本地 html 视频。
I recive the allow request to access camera but after accept i see just an empty page , my js file is like following:我收到允许访问相机的请求,但接受后我只看到一个空白页面,我的 js 文件如下所示:
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
this.localVideoref = React.createRef();
}
render() {
const constraints = { video: true };
const success = stream => {
this.localVideoref.current.srcObject = stream;
};
const failure = e => {
console.log("getUserMedia Error: ", e);
};
navigator.mediaDevices.getUserMedia(constraints, success, failure);
return (
<div>
<video ref={this.localVideoref} autoPlay />
</div>
);
}
}
export default App;
Does anyone know, what I have to adjust to make this work?有谁知道,我必须调整什么才能使这项工作?
Thanks!谢谢!
I think this will work:我认为这会奏效:
class App extends React.Component {
constructor(props) {
super(props);
this.localVideoref = React.createRef();
}
componentDidMount() {
const constraints = { video: true };
navigator.mediaDevices
.getUserMedia(constraints)
.then(res => {
this.localVideoref.current.srcObject = res;
});
}
render() {
return (
<div>
<video ref={this.localVideoref} autoPlay></video>
</div>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.