From 5bc848ed5f69d7f3d2df20357ce2a3e418ab8d84 Mon Sep 17 00:00:00 2001 From: Jorrin Date: Mon, 12 Feb 2024 18:47:20 +0100 Subject: [PATCH] add play and seek buttons --- apps/expo/src/app/videoPlayer.tsx | 2 ++ apps/expo/src/components/player/Controls.tsx | 19 ++++++++--- apps/expo/src/components/player/Header.tsx | 14 ++++---- .../src/components/player/MiddleButtons.tsx | 21 ++++++++++++ .../expo/src/components/player/PlayButton.tsx | 29 +++++++++++++++++ .../expo/src/components/player/SeekButton.tsx | 32 +++++++++++++++++++ 6 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 apps/expo/src/components/player/MiddleButtons.tsx create mode 100644 apps/expo/src/components/player/PlayButton.tsx create mode 100644 apps/expo/src/components/player/SeekButton.tsx diff --git a/apps/expo/src/app/videoPlayer.tsx b/apps/expo/src/app/videoPlayer.tsx index b09bb5b..2768219 100644 --- a/apps/expo/src/app/videoPlayer.tsx +++ b/apps/expo/src/app/videoPlayer.tsx @@ -13,6 +13,7 @@ import { fetchMediaDetails } from "@movie-web/tmdb"; import type { ItemData } from "~/components/item/item"; import { Header } from "~/components/player/Header"; +import { MiddleControls } from "~/components/player/MiddleButtons"; import { usePlayerStore } from "~/stores/player/store"; export default function VideoPlayerWrapper() { @@ -145,6 +146,7 @@ const VideoPlayer: React.FC = ({ data }) => { /> {isLoading && } {!isLoading && data &&
} + {!isLoading && } ); }; diff --git a/apps/expo/src/components/player/Controls.tsx b/apps/expo/src/components/player/Controls.tsx index cd4ef5d..3c92a87 100644 --- a/apps/expo/src/components/player/Controls.tsx +++ b/apps/expo/src/components/player/Controls.tsx @@ -1,13 +1,24 @@ -import { View } from "react-native"; +import React from "react"; +import { TouchableOpacity } from "react-native"; import { usePlayerStore } from "~/stores/player/store"; -interface ControlsProps { +interface ControlsProps extends React.ComponentProps { children: React.ReactNode; } -export const Controls = ({ children }: ControlsProps) => { +export const Controls = ({ children, className }: ControlsProps) => { const idle = usePlayerStore((state) => state.interface.isIdle); + const setIsIdle = usePlayerStore((state) => state.setIsIdle); - return {!idle && children}; + return ( + { + setIsIdle(false); + }} + className={className} + > + {!idle && children} + + ); }; diff --git a/apps/expo/src/components/player/Header.tsx b/apps/expo/src/components/player/Header.tsx index 8c25fb0..c8630aa 100644 --- a/apps/expo/src/components/player/Header.tsx +++ b/apps/expo/src/components/player/Header.tsx @@ -11,14 +11,12 @@ interface HeaderProps { export const Header = ({ title }: HeaderProps) => { return ( - - - - {title} - - - movie-web - + + + {title} + + + movie-web ); diff --git a/apps/expo/src/components/player/MiddleButtons.tsx b/apps/expo/src/components/player/MiddleButtons.tsx new file mode 100644 index 0000000..42e769a --- /dev/null +++ b/apps/expo/src/components/player/MiddleButtons.tsx @@ -0,0 +1,21 @@ +import { View } from "react-native"; + +import { Controls } from "./Controls"; +import { PlayButton } from "./PlayButton"; +import { SeekButton } from "./SeekButton"; + +export const MiddleControls = () => { + return ( + + + + + + + + + + + + ); +}; diff --git a/apps/expo/src/components/player/PlayButton.tsx b/apps/expo/src/components/player/PlayButton.tsx new file mode 100644 index 0000000..c480292 --- /dev/null +++ b/apps/expo/src/components/player/PlayButton.tsx @@ -0,0 +1,29 @@ +import { FontAwesome } from "@expo/vector-icons"; + +import { usePlayerStore } from "~/stores/player/store"; + +export const PlayButton = () => { + const videoRef = usePlayerStore((state) => state.videoRef); + const status = usePlayerStore((state) => state.status); + + return ( + { + if (status?.isLoaded) { + if (status.isPlaying) { + videoRef?.pauseAsync().catch(() => { + console.log("Error pausing video"); + }); + } else { + videoRef?.playAsync().catch(() => { + console.log("Error playing video"); + }); + } + } + }} + /> + ); +}; diff --git a/apps/expo/src/components/player/SeekButton.tsx b/apps/expo/src/components/player/SeekButton.tsx new file mode 100644 index 0000000..f6a2fc4 --- /dev/null +++ b/apps/expo/src/components/player/SeekButton.tsx @@ -0,0 +1,32 @@ +import { MaterialIcons } from "@expo/vector-icons"; + +import { usePlayerStore } from "~/stores/player/store"; + +interface SeekProps { + type: "forward" | "backward"; +} + +export const SeekButton = ({ type }: SeekProps) => { + const videoRef = usePlayerStore((state) => state.videoRef); + const status = usePlayerStore((state) => state.status); + + return ( + { + if (status?.isLoaded) { + const position = + type === "forward" + ? status.positionMillis + 10000 + : status.positionMillis - 10000; + + videoRef?.setPositionAsync(position).catch(() => { + console.log("Error seeking backwards"); + }); + } + }} + /> + ); +};