From 38445c6959199d7e171a2140c74b399927489eb9 Mon Sep 17 00:00:00 2001 From: Alex Kim Date: Sat, 7 Dec 2024 05:41:46 +1100 Subject: [PATCH] Fixed issue for IOS and android --- app/(auth)/player/direct-player.tsx | 46 +++++++++++++++++++- modules/vlc-player/ios/VlcPlayerModule.swift | 4 +- modules/vlc-player/ios/VlcPlayerView.swift | 10 ++--- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/app/(auth)/player/direct-player.tsx b/app/(auth)/player/direct-player.tsx index e1c88490..d534809c 100644 --- a/app/(auth)/player/direct-player.tsx +++ b/app/(auth)/player/direct-player.tsx @@ -30,8 +30,20 @@ import { useQuery } from "@tanstack/react-query"; import * as Haptics from "expo-haptics"; import { useFocusEffect, useGlobalSearchParams } from "expo-router"; import { useAtomValue } from "jotai"; -import React, { useCallback, useMemo, useRef, useState } from "react"; -import { Alert, BackHandler, View } from "react-native"; +import React, { + useCallback, + useMemo, + useRef, + useState, + useEffect, +} from "react"; +import { + Alert, + BackHandler, + View, + AppState, + AppStateStatus, +} from "react-native"; import { useSharedValue } from "react-native-reanimated"; import settings from "../(tabs)/(home)/settings"; import { useSettings } from "@/utils/atoms/settings"; @@ -359,6 +371,36 @@ export default function page() { }; }, []) ); + + const [appState, setAppState] = useState(AppState.currentState); + + useEffect(() => { + const handleAppStateChange = (nextAppState: AppStateStatus) => { + if (appState.match(/inactive|background/) && nextAppState === "active") { + console.log("App has come to the foreground!"); + // Handle app coming to the foreground + } else if (nextAppState.match(/inactive|background/)) { + console.log("App has gone to the background!"); + // Handle app going to the background + if (videoRef.current && videoRef.current.pause) { + videoRef.current.pause(); + } + } + setAppState(nextAppState); + }; + + // Use AppState.addEventListener and return a cleanup function + const subscription = AppState.addEventListener( + "change", + handleAppStateChange + ); + + return () => { + // Cleanup the event listener when the component is unmounted + subscription.remove(); + }; + }, [appState]); + // Preselection of audio and subtitle tracks. if (!settings) return null; diff --git a/modules/vlc-player/ios/VlcPlayerModule.swift b/modules/vlc-player/ios/VlcPlayerModule.swift index 64d6cad5..131f4ebf 100644 --- a/modules/vlc-player/ios/VlcPlayerModule.swift +++ b/modules/vlc-player/ios/VlcPlayerModule.swift @@ -5,7 +5,9 @@ public class VlcPlayerModule: Module { Name("VlcPlayer") View(VlcPlayerView.self) { Prop("source") { (view: VlcPlayerView, source: [String: Any]) in - view.setSource(source) + if !view.hasSource { + view.setSource(source) + } } Prop("paused") { (view: VlcPlayerView, paused: Bool) in diff --git a/modules/vlc-player/ios/VlcPlayerView.swift b/modules/vlc-player/ios/VlcPlayerView.swift index 59559dbb..8feec050 100644 --- a/modules/vlc-player/ios/VlcPlayerView.swift +++ b/modules/vlc-player/ios/VlcPlayerView.swift @@ -16,6 +16,7 @@ class VlcPlayerView: ExpoView { private var externalTrack: [String: String]? private var progressTimer: DispatchSourceTimer? private var isStopping: Bool = false // Define isStopping here + var hasSource = false // MARK: - Initialization @@ -157,6 +158,7 @@ class VlcPlayerView: ExpoView { self.setSubtitleTrack(subtitleTrackIndex) self.mediaPlayer?.media = media + hasSource = true if autoplay { print("Playing...") @@ -280,15 +282,11 @@ class VlcPlayerView: ExpoView { // MARK: - Private Methods @objc private func applicationWillResignActive() { - if !isPaused { - pause() - } + } @objc private func applicationDidBecomeActive() { - if !isPaused { - play() - } + } private func performStop(completion: (() -> Void)? = nil) {