gpt4 book ai didi

javascript - 在 React 中使用 Vidyard 嵌入式播放器

转载 作者:行者123 更新时间:2023-12-01 16:10:43 24 4
gpt4 key购买 nike

我的任务是将嵌入的“Vidyard”视频插入到 React 项目中。

嵌入代码看起来像这样。

<script type="text/javascript" async 
src="https://play.vidyard.com/embed/v4.js">
</script>
<img
style="width: 100%; margin: auto; display: block;"
class="vidyard-player-embed"
src="xxxx"
data-uuid="xxxx"
data-v="4"
data-type="inline"
/>

这在 100% 的情况下都不起作用。
当我将它插入一个普通的 .html文件,视频显示。

平原上 .html文件,图像加载,我相信脚本会找到图像并从那里将嵌入式视频播放器放到 DOM 上。

我认为正在发生的是 React/Vidyard 之间在操作 DOM 方面存在冲突。

我试图将其直接放入组件中,但没有运气。
我还将脚本调用直接添加到 <head> .
我关注了 Script load in react并试图手动调用脚本文件,我还将手动脚本加载卡在 componentWillMountcomponentDidMount .
我还将代码插入到 dangerouslySetInnerHTML .

行为保持不变。

理想情况下,如果这是可行的,图像将附加到页面上。 embed/v4.js然后脚本会在图像上附加一个嵌入式视频播放器。目前,图像出现并且没有切换到视频。如果我刷新/硬刷新页面,有时图像会换成嵌入式视频播放器。

最佳答案

对于那些喜欢钩子(Hook)/功能组件的人,这是我的解决方案:

import React from "react"
import ScriptLoader from "react-script-loader-hoc"

const VIDYARD_EMBED_JS_URL = "https://play.vidyard.com/embed/v4.js"

function VidyardPlayer(props: {
scriptsLoadedSuccessfully: boolean
maxWidth: string
maxHeight: string
type: string
uuid: string
aspect?: "4:3" | "16:9" | "16:10"
onLoadCallback?: (player: any, embedApi: any) => void
}) {
const {
scriptsLoadedSuccessfully,
maxWidth,
maxHeight,
type,
uuid,
aspect,
onLoadCallback,
} = props
const containerRef = React.useRef<any>()

React.useEffect(() => {
if (scriptsLoadedSuccessfully) {
window.VidyardV4.api
.renderPlayer({
aspect,
container: containerRef.current,
height: maxHeight,
type,
uuid,
width: maxWidth,
})
.then((player: any) => {
if (onLoadCallback) {
onLoadCallback(player, window.VidyardV4.api)
}
})
.catch((e: any) => {
// eslint-disable-next-line no-console
console.error(e.message)
})
}
}, [scriptsLoadedSuccessfully])

return <div ref={containerRef} />
}

export default ScriptLoader(VIDYARD_EMBED_JS_URL)(VidyardPlayer)

关于javascript - 在 React 中使用 Vidyard 嵌入式播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57827424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com