I was trying to create an app that loads videos from an ARRAY on loop. I am using UseState to change the URI index after the video ends.
The player works perfectly for around 1-2 hours then it freezes the video and throws onError() which gives player error null
and sometimes Player release time out.
I am thinking that it might be happening because of useState because it re-renders the component every time when index changes. Maybe this could be memory leakage or something else I am not sure.
please help me I seriously spent a week to find the solution. I even tried caching the videos using the filesystem. but still, it doesn't stop freezing.
const VideoPlayer = ({ wholeResult }) => {
const focuspoint = React.useRef(null);
const [index, setIndex] = React.useState(0);
const [progress, setProgress] = React.useState(false);
React.useEffect(() => {
if (wholeResult !== undefined) {
setProgress(true);
}
}, []);
const navigation = useNavigation();
return (
<View style={styles.container}>
<TouchableOpacity
onLongPress={() => {
navigation.navigate("Home");
}}
delayLongPress={3000}
>
{progress &&
wholeResult[index] !== "" &&
wholeResult[index] !== undefined
? <Video
ref={focuspoint}
style={styles.video}
source={{
uri: wholeResult.length == 1 ? wholeResult[0] : wholeResult[index],
}}
useNativeControls={false}
shouldPlay
resizeMode="stretch"
isLooping={wholeResult.length == 1 ? true : false}
onError={(error) =>
alert(error)
}
onPlaybackStatusUpdate={(status) =>
status?.didJustFinish == true
? setIndex((idx) => (idx == wholeResult.length - 1 ? 0 : idx + 1))
: null
}
/>
: null}
</TouchableOpacity>
</View>
);
};
export default VideoPlayer;
EDIT: I think this is the issue with expo-av only or it might be a bug with it. I tried with imageBackground and it's working is perfectly with the same pattern of code.