Skip to content

Commit

Permalink
fix: iOS safari playback issues
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemcilroy committed Apr 3, 2024
1 parent 55e9e6b commit b530b1f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions apps/web/app/s/[videoId]/_components/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ export const VideoPlayer = memo(

useImperativeHandle(ref, () => videoRef.current as HTMLVideoElement);

const isIOS = () =>
/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

const initializeHls = (
src: string,
media: HTMLMediaElement,
hlsInstance: React.MutableRefObject<Hls | null>
) => {
if (Hls.isSupported()) {
if (Hls.isSupported() && !isIOS()) {
const hls = new Hls({ progressive: true });
hls.on(Hls.Events.ERROR, (event, data) => {
console.error("HLS error:", data);
Expand All @@ -36,7 +39,6 @@ export const VideoPlayer = memo(
hls.recoverMediaError();
break;
default:
initializeFallback(src, media);
break;
}
}
Expand All @@ -46,17 +48,18 @@ export const VideoPlayer = memo(
hls.loadSource(src);
hls.attachMedia(media);
} else {
initializeFallback(src, media);
media.src = src;
}
};

const initializeFallback = (src: string, media: HTMLMediaElement) => {
media.src = src;
};

useEffect(() => {
if (!videoRef.current) return;
initializeHls(videoSrc, videoRef.current, videoHlsInstance);

if (isIOS()) {
videoRef.current.src = videoSrc;
} else {
initializeHls(videoSrc, videoRef.current, videoHlsInstance);
}

return () => {
videoHlsInstance.current?.destroy();
Expand Down

1 comment on commit b530b1f

@vercel
Copy link

@vercel vercel bot commented on b530b1f Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.