Skip to content

Commit

Permalink
fix: play next sound on video mod
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-r committed Feb 20, 2024
1 parent 4aea351 commit 7d5be4a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/components/VideoIframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import Player from "video.js/dist/types/player";
import "video.js/dist/video-js.css";
import "videojs-youtube";

import { usePlayVideo } from "../hooks/usePlayVideo";
import {
usePlayerAudio,
usePlayerState,
usePlayerVideo,
} from "../providers/Player";
import { useSetPlayerMode } from "../providers/PlayerMode";
import { usePreviousNextVideos } from "../providers/PreviousNextTrack";
import { useSetVideoIframeVisibility } from "../providers/VideoIframeVisibility";
import { ModalVideoIframeInformation } from "./ModalVideoIframeInformation";
import classes from "./VideoIframe.module.css";
Expand Down Expand Up @@ -44,6 +46,9 @@ const Video = ({ loop, src }: { loop: boolean; src: string }) => {
const videoRef = useRef();
const playerRef = useRef<Player>();
const playerAudio = usePlayerAudio();
const { videosIds } = usePreviousNextVideos();
const { handlePlay: play } = usePlayVideo();
const setPlayerMode = useSetPlayerMode();

const onReady = useCallback(
(player: Player) => {
Expand All @@ -56,6 +61,15 @@ const Video = ({ loop, src }: { loop: boolean; src: string }) => {
[playerAudio],
);

const onEnded = useCallback(() => {
// @ts-ignore
const audio = playerAudio?.current?.audioEl.current as HTMLAudioElement;
if (!audio.loop && videosIds.nextVideoId) {
setPlayerMode("audio");
play(videosIds.nextVideoId);
}
}, [play, playerAudio, setPlayerMode, videosIds.nextVideoId]);

const options = useMemo(
() => ({
techOrder: ["youtube"],
Expand Down Expand Up @@ -89,12 +103,16 @@ const Video = ({ loop, src }: { loop: boolean; src: string }) => {
const player = (playerRef.current = videojs(videoElement, options, () => {
onReady(player);
}));

player.on("ended", () => {
onEnded();
});
} else {
const player = playerRef.current as any;
player.autoplay(options.autoplay);
player.src(options.sources);
}
}, [onReady, options, videoRef]);
}, [onEnded, onReady, options, videoRef]);

useEffect(() => {
const player = playerRef.current;
Expand Down

0 comments on commit 7d5be4a

Please sign in to comment.