Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed ytPlayer issue in Vadim's Stream #2719

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client';
import React from 'react';
import { Box } from '@/components/ui/box';
import { useEffect } from 'react';
import React, { useEffect } from 'react';

declare global {
interface Window {
Expand All @@ -12,70 +11,86 @@ declare global {

const VadimStream = () => {
useEffect(() => {
let player1: any = null;
let player2: any = null;

// Load the IFrame Player API code asynchronously
const loadYouTubeAPI = () => {
// Only load the API if it hasn't been loaded yet
if (!document.getElementById('youtube-api')) {
const tag = document.createElement('script');
tag.id = 'youtube-api';
tag.src = 'https://www.youtube.com/iframe_api';
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode?.insertBefore(tag, firstScriptTag);
};
}

// This function creates an <iframe> (and YouTube player) after the API code downloads
window.onYouTubeIframeAPIReady = () => {
player1 = new window.YT.Player('player1', {
height: '100%',
width: '100%',
videoId: 'FBXUPJ9_Xl0',
playerVars: {
playsinline: 1,
},
// Initialize players when API is ready
const initializePlayers = () => {
new window.YT.Player('player1', {
events: {
onStateChange: (event: any) => {
if (event.data === window.YT.PlayerState.PLAYING) {
player2?.pauseVideo();
const player2Frame = document.getElementById(
'player2'
) as HTMLIFrameElement;
player2Frame?.contentWindow?.postMessage(
'{"event":"command","func":"pauseVideo","args":""}',
'*'
);
}
},
},
});

player2 = new window.YT.Player('player2', {
height: '100%',
width: '100%',
videoId: '9ErkOcDWJxI',
playerVars: {
playsinline: 1,
},
new window.YT.Player('player2', {
events: {
onStateChange: (event: any) => {
if (event.data === window.YT.PlayerState.PLAYING) {
player1?.pauseVideo();
const player1Frame = document.getElementById(
'player1'
) as HTMLIFrameElement;
player1Frame?.contentWindow?.postMessage(
'{"event":"command","func":"pauseVideo","args":""}',
'*'
);
}
},
},
});
};

loadYouTubeAPI();
// If API is already loaded, initialize players directly
if (window.YT && window.YT.Player) {
initializePlayers();
} else {
// Otherwise wait for API to load
window.onYouTubeIframeAPIReady = initializePlayers;
}

return () => {
player1?.destroy();
player2?.destroy();
// Only reset the ready handler, don't remove the script
window.onYouTubeIframeAPIReady = () => {};
};
}, []);

return (
<Box className="relative flex-1 w-full h-full gap-5 flex-col md:flex-row mb-auto">
<Box
className="flex-1 border border-outline-200 rounded-lg overflow-hidden aspect-video"
id="player1"
/>
<Box
className="flex-1 border border-outline-200 rounded-lg overflow-hidden aspect-video"
id="player2"
/>
<Box className="relative flex-1 w-full h-full gap-5 flex-col md:flex-row">
<Box className="flex-1 border border-outline-200 rounded-lg overflow-hidden aspect-video">
<iframe
id="player1"
width="100%"
height="100%"
src="https://www.youtube.com/embed/FBXUPJ9_Xl0?enablejsapi=1"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
/>
</Box>
<Box className="flex-1 border border-outline-200 rounded-lg overflow-hidden aspect-video">
<iframe
id="player2"
width="100%"
height="100%"
src="https://www.youtube.com/embed/9ErkOcDWJxI?enablejsapi=1"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
/>
</Box>
</Box>
);
};
Expand Down
Loading