From a4f0c9c753ec8844eda094080d81fbba566de7df Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Fri, 9 Aug 2024 03:24:38 +0100 Subject: [PATCH] [Video] more minor tweaks (#4906) * update expo-video * pause when on a different screen * rm collapsable * add mute/unmute button --------- Co-authored-by: Samuel Newman <10959775+mozzius@users.noreply.github.com> --- package.json | 2 +- .../VideoEmbedInner/VideoEmbedInnerNative.tsx | 76 ++++++++++++++++--- yarn.lock | 8 +- 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index faeee448..7c6e13af 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,7 @@ "expo-system-ui": "~3.0.4", "expo-task-manager": "~11.8.1", "expo-updates": "~0.25.14", - "expo-video": "^1.1.10", + "expo-video": "^1.2.4", "expo-web-browser": "~13.0.3", "fast-text-encoding": "^1.0.6", "history": "^5.3.0", diff --git a/src/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative.tsx b/src/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative.tsx index b95b9c80..26f6111b 100644 --- a/src/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative.tsx +++ b/src/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative.tsx @@ -1,18 +1,37 @@ -import React, {useEffect, useRef, useState} from 'react' +import React, {useCallback, useEffect, useRef, useState} from 'react' import {Pressable, View} from 'react-native' import Animated, {FadeIn} from 'react-native-reanimated' import {VideoPlayer, VideoView} from 'expo-video' +import {msg} from '@lingui/macro' +import {useLingui} from '@lingui/react' +import {useIsFocused} from '@react-navigation/native' -import {useVideoPlayer} from 'view/com/util/post-embeds/VideoPlayerContext' -import {android, atoms as a} from '#/alf' +import {HITSLOP_30} from '#/lib/constants' +import {useVideoPlayer} from '#/view/com/util/post-embeds/VideoPlayerContext' +import {android, atoms as a, useTheme} from '#/alf' +import {Mute_Stroke2_Corner0_Rounded as MuteIcon} from '#/components/icons/Mute' +import {SpeakerVolumeFull_Stroke2_Corner0_Rounded as UnmuteIcon} from '#/components/icons/Speaker' import {Text} from '#/components/Typography' export function VideoEmbedInnerNative() { const player = useVideoPlayer() const ref = useRef(null) + const isScreenFocused = useIsFocused() + + // pause the video when the screen is not focused + useEffect(() => { + if (!isScreenFocused) { + let wasPlaying = player.playing + player.pause() + + return () => { + if (wasPlaying) player.play() + } + } + }, [isScreenFocused, player]) return ( - + void }) { + const {_} = useLingui() + const t = useTheme() + const [isMuted, setIsMuted] = useState(player.muted) const [duration, setDuration] = useState(() => Math.floor(player.duration)) const [currentTime, setCurrentTime] = useState(() => Math.floor(player.currentTime), @@ -55,11 +77,21 @@ function Controls({ // 1000 gets out of sync with the video time }, 250) + // eslint-disable-next-line @typescript-eslint/no-shadow + const sub = player.addListener('volumeChange', ({isMuted}) => { + setIsMuted(isMuted) + }) + return () => { clearInterval(interval) + sub.remove() } }, [player]) + const toggleSound = useCallback(() => { + player.muted = !player.muted + }, [player]) + return ( {!isNaN(timeRemaining) && ( @@ -74,12 +106,13 @@ function Controls({ position: 'absolute', left: 5, bottom: 5, + minHeight: 20, + justifyContent: 'center', }, - ]} - pointerEvents="none"> + ]}> @@ -90,10 +123,35 @@ function Controls({ + + + {isMuted ? ( + + ) : ( + + )} + + ) } diff --git a/yarn.lock b/yarn.lock index 16547aa3..ba1227f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12302,10 +12302,10 @@ expo-updates@~0.25.14: ignore "^5.3.1" resolve-from "^5.0.0" -expo-video@^1.1.10: - version "1.1.10" - resolved "https://registry.yarnpkg.com/expo-video/-/expo-video-1.1.10.tgz#b47c0d40c21f401236639424bd25d70c09316b7b" - integrity sha512-k9ecpgtwAK8Ut8enm8Jv398XkB/uVOyLLqk80M/d8pH9EN5CVrBQ7iEzWlR3quvVUFM7Uf5wRukJ4hk3mZ8NCg== +expo-video@^1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/expo-video/-/expo-video-1.2.4.tgz#787342aded4295a1b6864f59227d178b93e1bb53" + integrity sha512-pBK9mt7vYAbuPQjCSQxHQ7xrNjbmRheJep7JIStEg57O183/JRfP2blKuXniiSt1HBdZYPdoQnGRa3jGMXB9pg== expo-web-browser@~13.0.3: version "13.0.3"