reactjs - SPFx 在 Web 部件上动态播放 HTML5 视频
问题描述
我们正在开发一个网络部件
它有一个文本框,我们在其中提供视频 URL
这将在按钮单击时从 SharePoint 列表中获取视频 URL 并设置视频元素的 SRC,下面是代码
function textFieldChanged(newValue: string){ let vidplay = document.getElementById('VidPlayer'); let source = document.createElement('source'); source.setAttribute('src', newValue); vidplay.appendChild(source); vidplay.load(); vidplay.play(); sp.web.currentUser.get().then((user: ISiteUserInfo) => { console.log("user", user); const userId: number = user.Id; sp.web.lists.getByTitle(videoList). items.select("ID", "User/ID", "VideoURL") .expand("User") .filter(`VideoURL eq '${decodeURIComponent(newValue)}' and User/ID eq '${userId}'`) .top(1) .get() .then((videos: IVideoEntry[]) => { console.log("Videos", videos); // Did we find a video? if (videos.length > 0) { //Update the record const video: IVideoEntry = videos[0]; console.log("We found the video", video); alert(videoRef.current.currentTime); } }); });
}
视频播放器元素
<div>
<video
id="VidPlayer"
ref={videoRef}
style={{ width: elementWidth }}
src={txtParam}
//controls
></video>< br/>
<TextField id="txtVideoURL" onChange={(ev,newValue)=> {txtParam=newValue}}/>
<DefaultButton text="Submit" onClick={()=>{textFieldChanged(txtParam)}}/>
</div>
阅读了很多与此相关的文章,但是无法使用加载视频元素然后播放所需的 .play() 和 .load() 。
不知道这需要如何在 SPFx 中完成,如果有人可以帮助我们,我将不胜感激。
如果需要任何其他详细信息,请告诉我
提前致谢。
解决方案
@维维克,
因为它是一个 React 项目,我建议你使用 ref 而不是 id。
private video_ref: React.RefObject<HTMLVideoElement>;
constructor(props: IPlayerProps) {
super(props);
this.videoRef= React.createRef();
}
textFieldChanged
我们可以通过如下方法获取它的实例:
private textFieldChanged(newValue: string) {
let vidplay = this.videoRef.current;
let source = document.createElement('source');
source.setAttribute('src', newValue);
vidplay.appendChild(source);
vidplay.load();
vidplay.play();
}
您也可以使用其他反应视频控件。
BR
推荐阅读
- php - Laravel 在 null 上调用成员函数 first()
- javascript - classList 的 JavaScript 问题
- python - 有谁知道如何更改函数,以便仅将生成的列表输出到 csv,而 df 中的每一行都不会被范围内的数字分割?
- jboss - 当我有 1000 多个规则时,如何优化我的 Kiebase 构建时间?
- python - 尝试使用 Scrapy 抓取时出现 555 HTTP 协议
- azure-devops - Task.json azure devops 扩展隐藏输入
- python - python中的异步错误
- hugo - 在 hugo 中替换 .Page.UniqueID 以解决弃用警告
- reactjs - 在表上使用 redux 是否值得?
- python - Python:相互依赖的类