I am using the new Expo Audio API for a react native project with expo ejected. When the audio player is used to the first time, i.e when the screen is first navigated to and before the audio is replaced, there is no error. Howevever, changing the audio or renavigating to the screen causes the error: Error: The 1st argument cannot be cast to type expo.modules.audio.AudioPlayer (received class java.lang.Integer)
.
Below is a snippet of my code
function LoadedPlayer({ book }: { book: book }) {
const bookProgress = useAppSelector(
(state) => state.books.bookProgress[book.id || ""]
);
let audioSource = {
uri: getAudioURL(book?.chapters[0].audio_id),
};
let AudioPlayer = useAudioPlayer(audioSource, 250);
const AudioPlayerStatus = useAudioPlayerStatus(AudioPlayer);
const sliderTouchableRef = useRef<View>(null);
const loadChapterToTrack = (chapterNumber: number) => {
if (!book.chapters || !book.chapters[chapterNumber] || !AudioPlayer) {
return;
}
try {
AudioPlayer.replace({
uri: getAudioURL(book.chapters[chapterNumber].audio_id),
});
} catch (e) {}
dispatch(
updateBookProgress({
bookId: book.id || "",
currentChapter: chapterNumber,
currentProgressSeconds: 0,
})
);
};
useEffect(() => {
setAudioModeAsync({
shouldPlayInBackground: true,
});
if (!bookProgress) {
dispatch(
updateBookProgress({
bookId: book.id || "",
currentChapter: 0,
currentProgressSeconds: 0,
})
);
}
(async () => {
if (!book.chapters) {
console.error("No chapters found for this book.");
return;
}
// Start playing it
AudioPlayer.play();
if (bookProgress?.currentProgressSeconds) {
AudioPlayer.seekTo(bookProgress.currentProgressSeconds);
}
})();
setTimeout(() => {
dispatch(addToCurrentlyReading(book.id || ""));
}, 5000);
setInterval(() => {
if (AudioPlayer.currentTime && AudioPlayer.duration) {
dispatch(
updateBookProgress({
bookId: book.id || "",
currentChapter: bookProgress?.currentChapter || 0,
currentProgressSeconds: AudioPlayer.currentTime,
})
);
}
}, 5000);
return () => {
AudioPlayer.release();
};
}, []);
useEffect(() => {
if (AudioPlayerStatus?.playbackState === "ended") {
console.log("ended");
loadChapterToTrack(bookProgress?.currentChapter + 1 || 0);
}
}, [AudioPlayerStatus.playbackState]);
return (
<ScreenContents />
);
}