Skip to content

Commit

Permalink
handle for all browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger committed Jun 14, 2024
1 parent 1c67e0d commit e4b754f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
30 changes: 26 additions & 4 deletions packages/core/src/buffer-until-first-frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,40 @@ export const useBufferUntilFirstFrame = ({
}

if (skipIfPaused && current.paused) {
console.log('not seeking because paused');
return;
}

bufferingRef.current = true;

const playback = delayPlayback();

current.requestVideoFrameCallback((_, info2) => {
console.log('requestVideoFrameCallback', _, info2);
bufferingRef.current = false;
const unblock = () => {
playback.unblock();
current.removeEventListener('ended', unblock, {
// @ts-expect-error
once: true,
});
current.removeEventListener('pause', unblock, {
// @ts-expect-error
once: true,
});
};

const onEndedOrPause = () => {
unblock();
};

current.requestVideoFrameCallback(() => {
// Safari often seeks and then stalls.
// This makes sure that the video actually starts playing.
current.requestVideoFrameCallback(() => {
bufferingRef.current = false;
unblock();
});
});

current.addEventListener('ended', onEndedOrPause, {once: true});
current.addEventListener('pause', onEndedOrPause, {once: true});
},
[delayPlayback, mediaRef, mediaType],
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/play-and-handle-not-allowed-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export const playAndHandleNotAllowedError = (
console.log(`Could not play ${mediaType} due to following error: `, err);
if (!current.muted) {
// eslint-disable-next-line no-console
console.log(`The video will be muted and we'll retry playing it.`, err);
console.log(`The video will be muted and we'll retry playing it.`);
current.muted = true;
current.play();
playAndHandleNotAllowedError(mediaRef, mediaType);
}
});
}
Expand Down
23 changes: 17 additions & 6 deletions packages/core/src/use-request-video-callback-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,39 @@ export const useRequestVideoCallbackTime = (
const currentTime = useRef<number | null>(null);

useEffect(() => {
if (mediaRef.current) {
currentTime.current = mediaRef.current.currentTime;
const {current} = mediaRef;
if (current) {
currentTime.current = current.currentTime;
} else {
currentTime.current = null;
return;
}

if (mediaType !== 'video') {
currentTime.current = null;
return;
}

const videoTag = current as HTMLVideoElement;

if (!videoTag.requestVideoFrameCallback) {
return;
}

let cancel = () => undefined;

const request = () => {
const cb = (
mediaRef.current as HTMLVideoElement
).requestVideoFrameCallback((_, info) => {
if (!videoTag) {
return;
}

const cb = videoTag.requestVideoFrameCallback((_, info) => {
currentTime.current = info.mediaTime;
request();
});

cancel = () => {
(mediaRef.current as HTMLVideoElement)?.cancelVideoFrameCallback(cb);
videoTag.cancelVideoFrameCallback(cb);
cancel = () => undefined;
};
};
Expand Down

0 comments on commit e4b754f

Please sign in to comment.