fix: stop playback on back button

This commit is contained in:
Fredrik Burmester
2024-12-01 14:20:00 +01:00
parent 383062ac0d
commit b9abe3e7f7
2 changed files with 27 additions and 5 deletions

View File

@@ -31,7 +31,7 @@ import * as Haptics from "expo-haptics";
import { useFocusEffect, useGlobalSearchParams } from "expo-router"; import { useFocusEffect, useGlobalSearchParams } from "expo-router";
import { useAtomValue } from "jotai"; import { useAtomValue } from "jotai";
import React, { useCallback, useMemo, useRef, useState } from "react"; import React, { useCallback, useMemo, useRef, useState } from "react";
import { Alert, View } from "react-native"; import { Alert, BackHandler, View } from "react-native";
import { useSharedValue } from "react-native-reanimated"; import { useSharedValue } from "react-native-reanimated";
export default function page() { export default function page() {
@@ -338,10 +338,22 @@ export default function page() {
: 0; : 0;
}, [item]); }, [item]);
const backAction = () => {
videoRef.current?.stop();
return false;
};
useFocusEffect( useFocusEffect(
React.useCallback(() => { React.useCallback(() => {
const onBackPress = () => {
return backAction();
};
BackHandler.addEventListener("hardwareBackPress", onBackPress);
return async () => { return async () => {
videoRef.current?.stop(); videoRef.current?.stop();
BackHandler.removeEventListener("hardwareBackPress", onBackPress);
}; };
}, []) }, [])
); );

View File

@@ -30,7 +30,7 @@ import React, {
useRef, useRef,
useState, useState,
} from "react"; } from "react";
import { View } from "react-native"; import { BackHandler, View } from "react-native";
import { useSharedValue } from "react-native-reanimated"; import { useSharedValue } from "react-native-reanimated";
import Video, { import Video, {
OnProgressData, OnProgressData,
@@ -369,13 +369,23 @@ const Player = () => {
})); }));
}; };
const backAction = () => {
videoRef.current?.pause();
return false;
};
useFocusEffect( useFocusEffect(
useCallback(() => { React.useCallback(() => {
const onBackPress = () => {
return backAction();
};
BackHandler.addEventListener("hardwareBackPress", onBackPress);
play(); play();
return () => { return async () => {
videoRef.current?.pause(); videoRef.current?.pause();
stop(); BackHandler.removeEventListener("hardwareBackPress", onBackPress);
}; };
}, []) }, [])
); );