简体   繁体   English

Cloudinary 视频集成反应找不到元素 ID

[英]Cloudinary video integration react could not find element id

I recently started integrating cloudinary player into React Web App.我最近开始将 cloudinary player 集成到 React Web App 中。 Now I am integrating this into existing player on other part of site and am running into "Uncaught Error: Could not find element with id some-video".现在我正在将它集成到站点其他部分的现有播放器中,并遇到“未捕获的错误:找不到 ID 为 some-video 的元素”。

componentDidMount() {
 //set video sources
 var source1 = { publicId: 'dog',  info: { title: 'My main movie', 
  subtitle: 'Something to know about the main movie' } }

//Create Cloudinary instance
 var cld = new Cloudinary({ cloud_name: "demo", secure: true });

//give video player cloudinary powers
var demoplayer = cld.videoPlayer("some-video", {
 publicId: {source1},
 width: 400,
height: 400,
 sourceTypes: ["mp4"],
});

}
 render() {
   var { video, user } = this.props;
   var loadedVideo = <></>;
   return (
     <Aux>
       <Grid container justify="center">
         {this.state.fetchDuration ? (
           <CircularProgress color="primary" />
         ) : (
           <Grid item xs={12} md={10}>
             <Paper style={useStyles.paper}>
               <Grid container justify="center">
                 <Grid item xs={12} md={8}>
                   {
                     <LazyLoad>
                       
                       <video
                         ref="videoRef"
                         id= "some-video"
                         onTimeUpdate={this.timestart}
                         onLoadedData={(e) => this.refs.videoRef.play()}
                         controls
                         autoplay
                         style={{ width: "100%", height: "auto" }}z
                       >
                         <source src={video.videoUrl} type="video/mp4" />
                       </video>
                     </LazyLoad>
</Aux>

Can you elaborate on why do you use some-video ?你能详细说明你为什么使用some-video吗? Instantiate the video player is bypassing the ID of the video tag ID you defined or the constructor parameters .实例化视频播放器会绕过您定义的视频标签 ID 或构造函数参数的ID。

Here is a code sample in React: https://codesandbox.io/s/video-player-forked-j3bgp And here are the steps to create it: https://cloudinary.com/documentation/video_player_how_to_embed#self_hosted_player这是 React 中的代码示例: https ://codesandbox.io/s/video-player-forked-j3bgp 以下是创建它的步骤: https : //cloudinary.com/documentation/video_player_how_to_embed#self_hosted_player

There are couple of problem.有几个问题。

  1. First, I see you are using a component named 'LazyLoader'.首先,我看到您正在使用名为“LazyLoader”的组件。 The 'video' element is a child of this component so you need to make sure the video element do not be loaded after the 'componentDidMount' does. 'video' 元素是该组件的子元素,因此您需要确保不会在 'componentDidMount' 加载之后加载视频元素。

  2. You should destory the player instance after the component get destroyed.您应该在组件被销毁后销毁播放器实例。 The Cloudinary Video-player is using Video.js : https://docs.videojs.com/tutorial-react.html Cloudinary 视频播放器正在使用 Video.js: https ://docs.videojs.com/tutorial-react.html

 componentDidMount() { this.player = cld.videoPlayer("some-video") } componentWillUnmount() { if (this.player) { this.player.dispose() } }
  1. the Cloudinery Core instance should be creadted once, you should move Cloudinery Core 实例应该创建一次,你应该移动

var cld = new Cloudinary({ cloud_name: "demo", secure: true }); var cld = new Cloudinary({ cloud_name: "demo", secure: true });

to you main App and pass it as a prop (or from a store) to the children components.给您的主应用程序并将其作为道具(或从商店)传递给子组件。

  1. 'cld.videoPlayer' can also can get an element and not only an id , so you can pass it 'videoRef' 'cld.videoPlayer' 也可以得到一个元素而不仅仅是一个 id ,所以你可以传递它 'videoRef'

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

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