Skip to content

Commit

Permalink
Page do not reload when Quality is changed for the video
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvilmehta committed Mar 14, 2024
1 parent 2bf6062 commit 20a12ad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/components/QualitySelectorControllBar.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import videojs from 'video.js';

const changeVideoQuality = (quality: string) => {
const changeVideoQuality = (quality: string, player: any) => {
const currentUrl = new URL(window.location.href);
currentUrl.searchParams.set('quality', quality);
window.location.href = currentUrl.href;
const newUrl = `${currentUrl.pathname}?${currentUrl.searchParams.toString()}`;
window.history.pushState({ path: newUrl }, '', newUrl);
player.qualitySelector(quality);
};

class QualitySelectorControllBar extends videojs.getComponent('Button') {
Expand Down Expand Up @@ -42,15 +44,16 @@ class QualitySelectorControllBar extends videojs.getComponent('Button') {
dropUpMenuElement.querySelectorAll('li').forEach((item) => {
item.addEventListener('click', (e: any) => {
const quality = e.target.getAttribute('data-quality');
if (quality) {
changeVideoQuality(quality);
const urlParams = new URLSearchParams(window.location.search);
if (quality !== urlParams.get('quality')) {
changeVideoQuality(quality, player);
}
dropUpMenuElement.style.display = 'none';
});
item.addEventListener('touchend', (e: any) => {
const quality = e.target.getAttribute('data-quality');
if (quality) {
changeVideoQuality(quality);
changeVideoQuality(quality, player);
}
dropUpMenuElement.style.display = 'none';
dropUpMenuElement.style.display = 'none';
Expand Down
11 changes: 11 additions & 0 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import './QualitySelectorControllBar';

// todo correct types
interface VideoPlayerProps {
setQuality: React.Dispatch<React.SetStateAction<number>>;
options: any;
onReady?: (player: Player) => void;
subtitles?: string;
Expand All @@ -26,6 +27,7 @@ const PLAYBACK_RATES: number[] = [0.5, 1, 1.25, 1.5, 1.75, 2];
const VOLUME_LEVELS: number[] = [0, 0.2, 0.4, 0.6, 0.8, 1.0];

export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
setQuality,
options,
contentId,
onReady,
Expand Down Expand Up @@ -231,6 +233,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
back: 15,
});

player.qualitySelector = setQuality;
const qualitySelector = player.controlBar.addChild(
'QualitySelectorControllBar',
);
Expand Down Expand Up @@ -266,6 +269,14 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
}
}, [options, onReady]);

useEffect(() => {
if (player) {
const currentTime = player.currentTime();
player.src(options.sources[0]);
player.currentTime(currentTime);
}
}, [options.sources[0]]);

useEffect(() => {
const player = playerRef.current;
return () => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/VideoPlayerSegment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Thumbnail {
}

interface VideoProps {
setQuality: React.Dispatch<React.SetStateAction<number>>;
thumbnails: Thumbnail[];
segments: Segment[];
subtitles: string;
Expand All @@ -27,6 +28,7 @@ interface VideoProps {
}

export const VideoPlayerSegment: FunctionComponent<VideoProps> = ({
setQuality,
contentId,
subtitles,
segments,
Expand Down Expand Up @@ -93,6 +95,7 @@ export const VideoPlayerSegment: FunctionComponent<VideoProps> = ({
className="hidden absolute bg-no-repeat bg-cover w-[320px] h-[180px] pointer-events-none z-10"
/>
<VideoPlayer
setQuality={setQuality}
contentId={contentId}
subtitles={subtitles}
options={videoJsOptions}
Expand Down
5 changes: 4 additions & 1 deletion src/components/admin/ContentRendererClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export const ContentRendererClient = ({
const router = useRouter();

//@ts-ignore
const quality: '720' | '1080' | '360' | null = searchParams.get('quality');
const [quality, setQuality] = useState<number>(
searchParams.get('quality') | null,
);

if (!metadata) {
return <div>Loading</div>;
Expand Down Expand Up @@ -92,6 +94,7 @@ export const ContentRendererClient = ({
<div className="flex gap-2 items-start flex-col lg:flex-row">
<div className="flex-1 w-full">
<VideoPlayerSegment
setQuality={setQuality}
contentId={content.id}
subtitles={metadata.subtitles}
thumbnails={[]}
Expand Down
11 changes: 7 additions & 4 deletions src/db/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export class Cache {
return this.instance;
}

set(type: string, args: string[], value: any, expirySeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10)) {
set(
type: string,
args: string[],
value: any,
expirySeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10),
) {
this.inMemoryDb.set(`${type} ${JSON.stringify(args)}`, {
value,
expiry: new Date().getTime() + expirySeconds * 1000,
Expand All @@ -46,7 +51,5 @@ export class Cache {
return entry.value;
}

evict() {

}
evict() {}
}

0 comments on commit 20a12ad

Please sign in to comment.