简体   繁体   English

如何在 React js 中使用 VdoCipher

[英]How To Use VdoCipher with React js

How to fetch videos from VdoCipher and display then on my React js WebApp?如何从 VdoCipher 获取视频并显示在我的 React js WebApp 上?

I am currently trying to use VdoCipher to store videos (I will upload them manually on the website) and then display them on my react webapp... infortuntly the documentation isn't very clear for me.我目前正在尝试使用 VdoCipher 来存储视频(我将在网站上手动上传它们),然后将它们显示在我的 React webapp 上……不幸的是,文档对我来说不是很清楚。

Here is some sample code to implement this这是一些示例代码来实现这个

import React, { Component } from "react";
import "./style.css";

class VideoPlayer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      videoObject: null
    };
  }

  componentDidMount() {
    if (window.VdoPlayer) {
      return this.loadPlayer();
    }
    const playerScript = document.createElement("script");
    playerScript.src =
      "https://player.vdocipher.com/playerAssets/1.6.10/vdo.js";
    document.body.appendChild(playerScript);
    playerScript.addEventListener("load", () => {
      return this.loadPlayer();
    });
  }

  loadPlayer() {
    window.playerContainer = this.refs.container;
    new window.VdoPlayer({
      otp: this.props.otp,
      playbackInfo: this.props.playbackInfo,
      theme: "9ae8bbe8dd964ddc9bdb932cca1cb59a",
      container: this.refs.container
    });
  }

  render() {
    return <div className="player" ref="container"></div>;
  }
}

export default VideoPlayer;

Codesandbox link for working version 工作版本的 Codesandbox 链接

Since there is a javascript src to be loaded, it checks in componentDidMount for the presence of global variable.由于要加载 javascript src,因此它会检查 componentDidMount 是否存在全局变量。 You can choose to use this by alternative means such as scriptjs or adding script tag the index.html template.您可以选择通过其他方式使用它,例如scriptjs或在 index.html 模板中添加脚本标签。

The return of the new VdoPlayer() is supposed to be the reference of player that you need to call javascript APIs on player. new VdoPlayer()的返回应该是播放器的引用,您需要在播放器上调用 javascript API。 If needed, you can make it available to the outside components with a callback prop.如果需要,您可以使用回调 prop 使其对外部组件可用。 And then call this callback prop after the new VdoPlayer()然后在new VdoPlayer()之后调用这个回调 prop

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

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