I'm trying to create a feature where a group of videos gets played automatically without user action using React Native's Expo AV package, specifically the Video element. Right now, the code is wired up so that when the video finishes, it changes the video. However, with the way it's wired up, some videos either skip entirely or play small snippets of the video before skipping.
Here are the parts of the code I want to fix:
const EpisodePlayerScreen = ({ route }) => {
const { episode }: { episode: Episode } = route.params;
const videos: VideoType[] = episode.videos;
const [currVideoIdx, setCurrVideoIdx] = useState(null);
const [uri, setUri] = useState(null);
const [videoStatus, setVideoStatus] = useState(null)
useEffect(() => {
if (typeof currVideoIdx === 'number' && currVideoIdx < videos.length) {
setUri(videos[currVideoIdx].uri);
} else if (currVideoIdx === videos.length) {
setUri(null);
}
}, [currVideoIdx]);
useEffect(() => {
if (videoStatus && videoStatus.didJustFinish) {
setCurrVideoIdx(idx => idx + 1)
}
}, [videoStatus])
return (
<SafeAreaView style={styles.container}>
{currVideoIdx === null
? <Text style={styles.text}>{episode.title}</Text>
: (currVideoIdx === videos.length ? <Text
style={styles.text}
onPress={resetEpisode}
>replay episode</Text>
: <Video
source={{ uri }}
shouldPlay
onPlaybackStatusUpdate={status => {
if (status.didJustFinish) setVideoStatus(() => status);
}}
/>)
}
</SafeAreaView>
);
};
Basically, the idea is that if the video finishes, we change the video and the uri. I've tried using other properties on the playback status (positionMillis and durationMillis) to no avail. I've had some luck checking if isPlaying is false but when I do that, it plays the first video but the second one doesn't play.