Skip to content

Commit

Permalink
Merge pull request #212 from RandomStudio/autoplay-issues
Browse files Browse the repository at this point in the history
adjust autoplay
  • Loading branch information
ewaperlinska authored Aug 29, 2023
2 parents 2b0f59f + 3430e69 commit b892d18
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/components/HomeVideo/HomeVideo.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

.videoWrapper {
width: 100%;
max-height: 100vh;
min-height: 60vh;

& video {
width: 100%;
height: auto;
min-height: 60vh;
min-height: 60svh;
max-height: 100vh;
max-height: 100svh;
height: 100%;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/LazyLoad/LazyLoad.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useRef, useState } from 'react';

type LazyLoadProps = {
children: React.ReactNode;
children: JSX.Element;
onIntersect?: () => void;
};

Expand Down
6 changes: 5 additions & 1 deletion src/components/Video/Controls/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ const Controls = ({

useEffect(() => {
if (isPlaying) {
videoRef.current?.play();
videoRef.current
.play()
.catch(e =>
console.warn('Unable to autoplay without user interaction', e),
);
} else {
videoRef.current?.pause();
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Video/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ const Video = forwardRef<HTMLVideoElement, VideoProps>(

const handleVideoReady = useCallback(() => {
setHasLoaded(true);
}, []);
onReady();
}, [onReady]);

const aspectRatioStyle = { aspectRatio: `${width} / ${height}` };

useHlsVideo({
isAutoplaying,
isMounted,
onReady,
onReady: handleVideoReady,
videoRef,
src: hls,
});
Expand Down Expand Up @@ -94,10 +95,9 @@ const Video = forwardRef<HTMLVideoElement, VideoProps>(
loop={isLooping}
muted
onClick={handleClick}
onPlaying={handleVideoReady}
playsInline
ref={videoRef}
src=""
src={hls}
style={aspectRatioStyle}
/>

Expand Down
37 changes: 22 additions & 15 deletions src/components/Video/useHlsVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ const useHlsVideo = ({
videoRef: MutableRefObject<HTMLVideoElement>;
}) => {
useEffect(() => {
if (!isMounted || !videoRef.current || !Hls.isSupported()) {
if (!isMounted || !videoRef.current) {
return;
}

// if HLS is natively supported, we don't have to do anything
if (videoRef.current.canPlayType('application/vnd.apple.mpegurl')) {
// eslint-disable-next-line no-param-reassign
videoRef.current.src = src;

videoRef.current.addEventListener('canplay', () => {
if (isAutoplaying) {
videoRef.current.play();
}
onReady();

onReady();
});
if (isAutoplaying) {
videoRef.current
.play()
.catch(e =>
console.warn('Unable to autoplay without user interaction', e),
);
}

return;
}
Expand All @@ -38,15 +38,22 @@ const useHlsVideo = ({
startLevel: window.innerWidth > 1280 ? 4 : 2,
});

hls.loadSource(src);
hls.attachMedia(videoRef.current);

hls.on(Hls.Events.MEDIA_ATTACHED, () => {
if (isAutoplaying) {
videoRef.current.play();
}
hls.loadSource(src);

onReady();
hls.on(Hls.Events.MANIFEST_PARSED, () => {
if (isAutoplaying) {
try {
videoRef.current.play();
} catch (e) {
console.warn('Unable to autoplay without user interaction');
}
}

onReady();
});
});
}, [isMounted, videoRef, onReady, isAutoplaying, src]);
};
Expand Down
6 changes: 5 additions & 1 deletion src/pages/video/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ const VideoFocusModePage = ({ video }: VideoFocusModePageProps) => {

const handleClick = useCallback(() => {
if (videoRef.current?.paused) {
videoRef.current?.play();
videoRef.current
.play()
.catch(e =>
console.warn('Unable to autoplay without user interaction', e),
);
} else {
videoRef.current?.pause();
}
Expand Down

0 comments on commit b892d18

Please sign in to comment.