简体   繁体   English

如何使用带有反应的 wistia 内联播放器?

[英]How can I use wistia inline player with react?

I am trying to load in an inline video player from wistia, and the docs say to use this code :我正在尝试从 wistia 加载内联视频播放器,并且文档说要使用此代码:

<script src="https://fast.wistia.com/embed/medias/<hashedid>.jsonp" async></script><script src="https://fast.wistia.com/assets/external/E-v1.js" async></script><div class="wistia_responsive_padding" style="padding:56.0% 0 0 0;position:relative;"><div class="wistia_responsive_wrapper" style="height:100%;left:0;position:absolute;top:0;width:100%;"><div class="wistia_embed wistia_async_<hashedid> videoFoam=true" style="height:100%;width:100%">&nbsp;</div></div></div>

I have changed the style syntax to jsx as well as class to className * see below我已将样式语法更改为 jsx,并将类更改为 className * 见下文

        <script src="https://fast.wistia.com/embed/medias/<hashed_id>.jsonp" async></script>
        <script src="https://fast.wistia.com/assets/external/E-v1.js" async></script>
        <div className="wistia_responsive_padding" style={{padding:"56.0% 0 0 0",position:"relative"}}>
        <div className="wistia_responsive_wrapper" style={{height:"100%",left:0,position:"absolute",top:0,width:"100%"}}>
        <div className="wistia_embed wistia_async_<hashed_id> videoFoam=true" style={{height:"100%",width:"100%"}}>

Still this isn't letting the browser render the player, I believe this may be due to the script tags, but am not sure how to fix this.这仍然没有让浏览器呈现播放器,我相信这可能是由于脚本标签造成的,但我不确定如何解决这个问题。 How can I make my video appear?如何让我的视频出现? * Note if I was to use regular html that inline video player would work for me & also I can't use the iframe code because that uses htm l5 player for mobile devices rather than wistia (atleast when I tried it) * 请注意,如果我要使用常规 html,内联视频播放器对我有用,而且我不能使用 iframe 代码,因为它使用 htm l5 播放器用于移动设备而不是 wistia(至少在我尝试时)

Create a separate component and import it.创建一个单独的组件并导入它。

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>
        );
    };
}

Here is a more up to date component you can use.这是您可以使用的更新组件。 The hashedId is a prop. 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>

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

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