Skip to content

Commit

Permalink
Merge pull request #461 from VK-RED/youtube-support
Browse files Browse the repository at this point in the history
feat: add Youtube Renderer in VideoPlayer
  • Loading branch information
hkirat authored Apr 20, 2024
2 parents 68b9b84 + cd5a114 commit e905c71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'videojs-seek-buttons';
import { handleMarkAsCompleted } from '@/lib/utils';
import { useSearchParams } from 'next/navigation';
import './QualitySelectorControllBar';
import { YoutubeRenderer } from './YoutubeRenderer';

// todo correct types
interface VideoPlayerProps {
Expand All @@ -38,6 +39,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
const playerRef = useRef<Player | null>(null);
const [player, setPlayer] = useState<any>(null);
const searchParams = useSearchParams();
const vidUrl = options.sources[0].src;
useEffect(() => {
const t = searchParams.get('timestamp');
if (contentId && player && !t) {
Expand Down Expand Up @@ -352,6 +354,16 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
player.currentTime(parseInt(t, 10));
}
}, [searchParams, player]);

const isYoutubeUrl = (url: string) => {
const regex = /^https:\/\/www\.youtube\.com\/embed\/[a-zA-Z0-9_-]+/;
return regex.test(url);
};

if (isYoutubeUrl(vidUrl)) {
return <YoutubeRenderer url={vidUrl} />;
}

return (
<div
data-vjs-player
Expand Down
23 changes: 23 additions & 0 deletions src/components/YoutubeRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FunctionComponent } from 'react';

interface YoutubeRendererProps {
url: string;
}

export const YoutubeRenderer: FunctionComponent<YoutubeRendererProps> = ({
url,
}) => {
return (
<div className="mt-2 flex justify-center">
<iframe
width="100%"
className="h-[80vh]"
height="500px"
src={url}
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen
></iframe>
</div>
);
};

0 comments on commit e905c71

Please sign in to comment.