如何使用wistia内联播放器做出反应?

时间:2017-07-27 18:04:50

标签: reactjs

我正在尝试加载来自wistia的内嵌视频播放器,文档说要使用此代码:

Jbo

我已将样式语法更改为jsx以及类更改为className *,请参阅下面的

if (everythingIsDefined(obj)) {
  console.log(obj.jbo.toString()) // no error
} 

这仍然不是让浏览器渲染播放器,我相信这可能是由于脚本标签,但我不知道如何解决这个问题。如何让我的视频出现? *请注意,如果我使用常规HTML,内联视频播放器将适用于我&我也不能使用iframe代码,因为它使用htm l5播放器用于移动设备而不是wistia(至少在我试用时)

2 个答案:

答案 0 :(得分:6)

创建一个单独的组件并导入它。

export default
class YourVideo extends React.Component {

componentWillMount() {
    const script1 = document.createElement("script");
    const script2 = document.createElement("script");

    script1.src = "https://fast.wistia.com/embed/medias/videolink.jsonp";
    script1.async = true;

    script2.src = "https://fast.wistia.com/assets/external/E-v1.js";
    script2.async = true;

    document.body.appendChild(script1);
    document.body.appendChild(script2);
}

render() {
    return (
        <div>
            <div className="wistia_embed wistia_async_videolink videoFoam=true"/>
        </div>
        );
    };
}

答案 1 :(得分:1)

这是您可以使用的更新组件。 hashedId 是一个道具。

import React, {Component} from 'react';

class WistiaEmbed extends Component {
  constructor(props) {
    super(props);
    const {hashedId, ...embedOptions} = {...this.props};
    if (typeof window !== `undefined`) {
      window._wq = window._wq || [];
      window._wq.push({
        id: hashedId,
        options: embedOptions,
        onHasData: (video) => {
          this.handle = video;
        },
      });
    }
  }

  _renderCommon() {
    const {hashedId} = this.props;
    return (
      <div
        class="wistia_swatch"
        style={{
          height: '100%',
          left: 0,
          opacity: 0,
          overflow: 'hidden',
          position: 'absolute',
          top: 0,
          transition: 'opacity 200ms',
          width: '100%',
        }}
      >
        <img
          src={`https://fast.wistia.com/embed/medias/${hashedId}/swatch`}
          style={{filter: 'blur(5px)', height: '100%', objectFit: 'contain', width: '100%'}}
          alt=""
          aria-hidden="true"
          onload="this.parentNode.style.opacity=1;"
        />
      </div>
    );
  }

  _renderResponsive() {
    const {hashedId, padding} = this.props;

    return (
      <div className="wistia_responsive_padding" style={{padding, position: 'relative'}}>
        <div
          className="wistia_responsive_wrapper"
          style={{height: '100%', left: '0', position: 'absolute', top: 0, width: '100%'}}
        >
          <div
            className={`wistia_embed wistia_async_${hashedId} videoFoam=true`}
            style={{height: '100%', width: '100%', position: 'relative'}}
          >
            {this._renderCommon()}
          </div>
        </div>
      </div>
    );
  }

  _renderFixed() {
    const {width, height, hashedId} = this.props;
    return (
      <div
        class={`wistia_embed wistia_async_${hashedId}`}
        style={`height:${height || 480}px;position:relative;width:${width || 640}px`}
      >
        {this._renderCommon()}
      </div>
    );
  }

  render() {
    const {isResponsive} = this.props;
    return isResponsive ? this._renderResponsive() : this._renderFixed;
  }

  componentDidMount() {
    if (!document.getElementById('wistia_script')) {
      var wistiaScript = document.createElement('script');
      wistiaScript.id = 'wistia_script';
      wistiaScript.type = 'text/javascript';
      wistiaScript.src = 'https://fast.wistia.com/assets/external/E-v1.js';
      wistiaScript.async = true;
      document.body.appendChild(wistiaScript);
    }
  }

  componentWillUnmount() {
    this.handle && this.handle.remove();
  }
}

WistiaEmbed.defaultProps = {
  isResponsive: true,
};

export default WistiaEmbed;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>