react-native-video: [BUG]: Crash OutOfMemoryError Android
Version
"react-native-video": "^6.10.0", "react-native": "0.76.5",
What platforms are you having the problem on?
Android
System Version
Android 15
On what device are you experiencing the issue?
Real device
Architecture
Old architecture
What happened?
I also frequently encounter this error on Android. When navigating from the list page to the details page to play a video, then returning to the previous page and entering the details page again to play the video, after repeating this process a few times, the memory usage explodes. The following is the error message: OutOfMemoryError Failed to allocate a 8208 byte allocation with 1238704 free bytes and 1209KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
Reproduction Link
No response
Reproduction
This bug only occurs on Android, while it works fine on iOS.
const VideoPlayer = ({ source, height, width, thumbnail, onLoadBack, autoplay }: Porps) => { const [paused, setPaused] = useState(autoplay); const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0); const [showControls, setShowControls] = useState(false); const [isFullscreen, setIsFullscreen] = useState(false); let [showThumbnail, setShowThumbnail] = useState(true);
let [videoLoading, setVideoLoading] = useState(false);
const videoRef = useRef<VideoRef>(null); const timeoutRef = useRef<NodeJS.Timeout>();
useEffect(() => { setPaused(autoplay); }, [autoplay]);
useEffect(() => { return () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } if (videoRef.current) { videoRef.current?.seek(0); } setPaused(true); }; }, []);
useEffect(() => {}, []);
useFocusEffect( React.useCallback(() => { return () => { setPaused(true); }; }, []) );
const toggleControls = () => { if (videoLoading) { return; }
setShowControls(true);
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(
() => {
setShowControls(false);
},
3000
);
};
const formatTime = (seconds: number) => {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return ${mins}:${secs.toString().padStart(2, '0')};
};
let onEnd = () => { setCurrentTime(0); setPaused(true); videoRef.current?.seek(0); };
const onFullscreenPlayerWillDismiss = () => { Orientation.lockToPortrait(); };
let onFullscreenPlayerDidPresent = () => { Orientation.lockToLandscapeLeft(); }; console.log(‘videoRef.current----->’, source);
const renderVideoPlayer = (fullscreen: boolean) => ( <TouchableWithoutFeedback onPress={toggleControls}> <View style={[styles.normalVideo, {width: width, height: height}]}> <Video ref={videoRef} source={{uri: source}} // source={{uri: ‘http://127.0.0.1:5500/123213123.mp4’}} style={[styles.normalVideo, {width: width, height: height}]} paused={paused} muted={false} resizeMode=“cover” volume={1} onEnd={onEnd} fullscreenAutorotate={false} fullscreen={false} repeat={Platform.OS === ‘android’ ? false : true} fullscreenOrientation={‘landscape’} onFullscreenPlayerWillDismiss={onFullscreenPlayerWillDismiss} onFullscreenPlayerDidPresent={onFullscreenPlayerDidPresent} onLoad={data => { onLoadBack && onLoadBack(); setDuration(data.duration); }} onLoadStart={() => setVideoLoading(true)} // onBuffer={({isBuffering}) => setVideoLoading(isBuffering)} onReadyForDisplay={() => setVideoLoading(false)} onError={error => { setVideoLoading(false); }} onProgress={data => setCurrentTime(data.currentTime)} />
{videoLoading && (
<View style={styles.loadingContainer}>
<VideoLoading />
</View>
)}
{showControls && (
<View style={styles.controls}>
<TouchableOpacity
style={[styles.centerButton, {height: height}]}
onPress={() => {
setPaused(!paused);
toggleControls();
}}>
{paused ? (
<Image
style={styles.playStyle}
source={require('../../image/buy/playing.png')}
/>
) : (
<Image
style={styles.playStyle}
source={require('../../image/buy/pause.png')}
/>
)}
</TouchableOpacity>
<View style={[styles.bottomBar, {width: width}]}>
<View style={styles.timeAndfullBtn}>
<Text style={styles.timeText}>
{formatTime(currentTime)}
<Text
style={[styles.timeText, {lineHeight: 12, fontSize: 9}]}>
{'/'}
</Text>
{formatTime(duration)}
</Text>
<TouchableOpacity
onPress={() => {
videoRef.current?.presentFullscreenPlayer();
setIsFullscreen(!isFullscreen);
}}
style={styles.fullscreenButton}>
<Image
style={styles.fullScreen}
source={require('../../image/buy/fullScreen.png')}
/>
</TouchableOpacity>
</View>
{/* <Text style={styles.timeText}>{formatTime(duration)}</Text> */}
</View>
</View>
)}
{showControls ? (
<Slider
style={[styles.slider, {bottom: -20}]}
minimumValue={0}
thumbImage={require('../../image/buy/Slider.png')}
maximumValue={duration}
value={currentTime}
onSlidingComplete={value => videoRef.current?.seek(value)}
minimumTrackTintColor="#fff"
maximumTrackTintColor="rgba(255,255,255,0.5)"
/>
) : null}
</View>
</TouchableWithoutFeedback>
);
// console.log(‘autoplay------>’, currentTime); return ( <View> {renderVideoPlayer(false)}
{showThumbnail && currentTime === 0 && autoplay ? (
<ImageBackground style={styles.thumbnail} source={{uri: thumbnail}}>
<TouchableOpacity
onPress={() => {
setShowThumbnail(false);
setPaused(false);
toggleControls();
}}>
<Image source={require('../../image/buy/playing.png')} />
</TouchableOpacity>
</ImageBackground>
) : null}
{/* {renderFullscreen()} */}
</View>
); };
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 20
I’m experiencing the same issue. I’ve tried updating to the latest version (6.12.0), but still encounter the same error.
I have the same issue “out-of-memory” when using Video on Android
repro from fresh project setup: https://github.com/dannyhw/video_memory_repro
heres a recording of the out of memory error, should be reproducible every time
https://github.com/user-attachments/assets/d7364bd8-c26a-4cc5-876b-a56b7e6b15f5
Notes:
This reproduction seems to almost entirely depend on the repeat flag being true
however if I make disableFocus true then it doesn’t seem to crash anymore even with repeat true, which is strange since all the videos are playing at once, but when just one is video playing its causing memory to increase until it crashes.
what is weirder to me is that in the app I’m using the library in repeat is false and crashes almost immediately.
in this repro repeating the video like this doesn’t cause the memory crash