-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #628 from us3r-network/F-frameVideoSupport-ttang
feat: video support
- Loading branch information
Showing
5 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
"unpinup", | ||
"upsert", | ||
"Upvote", | ||
"videojs", | ||
"viem", | ||
"Warpcast", | ||
"Whatsnew", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import VideoRenderer from './VideoRender'; | ||
|
||
export default function EmbedVideo({ videoUrl }: { videoUrl: string }) { | ||
return ( | ||
<div className="max-w-96 "> | ||
<VideoRenderer videoSrc={videoUrl} />; | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* eslint-disable react/destructuring-assignment */ | ||
|
||
import React, { useMemo, useEffect } from 'react'; | ||
import videojs from 'video.js'; | ||
|
||
import 'video.js/dist/video-js.css'; | ||
|
||
interface PlayerProps { | ||
videoSrc: string; | ||
} | ||
|
||
const videoJSoptions: { | ||
techOrder?: string[]; | ||
autoplay?: boolean; | ||
fill?: boolean; | ||
controls?: boolean; | ||
responsive?: true; | ||
fluid?: true; | ||
} = { | ||
fluid: true, | ||
controls: true, | ||
fill: true, | ||
responsive: true, | ||
}; | ||
|
||
function handleClick(e: any) { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
} | ||
|
||
export default function VideoRenderer(props: PlayerProps) { | ||
const videoRef = React.useRef<HTMLDivElement>(null); | ||
const playerRef = React.useRef<any>(null); | ||
|
||
const options = useMemo( | ||
() => ({ | ||
...videoJSoptions, | ||
// video is not necessarily rewritten yet | ||
sources: [ | ||
{ | ||
src: props.videoSrc ?? '', | ||
type: props.videoSrc?.endsWith('.m3u8') | ||
? 'application/x-mpegURL' | ||
: props.videoSrc?.endsWith('.mp4') | ||
? 'video/mp4' | ||
: '', | ||
}, | ||
], | ||
}), | ||
[props.videoSrc] | ||
); | ||
|
||
useEffect(() => { | ||
// Make sure Video.js player is only initialized once | ||
if (!playerRef.current) { | ||
// The Video.js player needs to be _inside_ the component el for React 18 Strict Mode. | ||
const videoElement = document.createElement('video-js'); | ||
|
||
videoElement.classList.add('vjs-big-play-centered'); | ||
videoRef.current?.appendChild(videoElement); | ||
|
||
playerRef.current = videojs(videoElement, options); | ||
} else { | ||
const player = playerRef.current; | ||
|
||
player.autoplay(options.autoplay); | ||
player.src(options.sources); | ||
} | ||
}, [options, videoRef, props]); | ||
|
||
// Dispose the Video.js player when the functional component unmounts | ||
useEffect(() => { | ||
const player = playerRef.current; | ||
|
||
return () => { | ||
if (player && !player.isDisposed()) { | ||
player.dispose(); | ||
playerRef.current = null; | ||
} | ||
}; | ||
}, [playerRef]); | ||
|
||
return ( | ||
<div | ||
data-vjs-player | ||
onClick={handleClick} | ||
className="rounded-md overflow-hidden" | ||
> | ||
<div ref={videoRef} /> | ||
</div> | ||
); | ||
} |