Fixed issue for IOS and android

This commit is contained in:
Alex Kim
2024-12-07 05:41:46 +11:00
parent 24320541c7
commit 38445c6959
3 changed files with 51 additions and 9 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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) {