Skip to content

Commit 0a0a3ec

Browse files
committed
feat: implemented audio preloading
Should make this cleaner in the future
1 parent b587642 commit 0a0a3ec

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

frontend/src/routes/wrapped.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ import dream4 from "../assets/wrapped/dream-4.mp3";
2525
import alive1 from "../assets/wrapped/alive-1.mp3";
2626
import alive2 from "../assets/wrapped/alive-2.mp3";
2727

28+
const AUDIO_ASSETS = [
29+
dancingQueen,
30+
franz1, franz2, franz3,
31+
dream1, dream2, dream3, dream4,
32+
alive1, alive2
33+
];
34+
35+
const preloadAudio = (src: string) => {
36+
return new Promise((resolve, reject) => {
37+
const audio = new Audio(src);
38+
audio.preload = "auto";
39+
40+
audio.oncanplaythrough = () => resolve(src);
41+
42+
audio.onerror = () => {
43+
console.warn(`Failed to load audio: ${src}`);
44+
resolve(null);
45+
};
46+
});
47+
};
48+
2849
const fetchWrappedData = async (id: string) => {
2950
try {
3051
const res = await axios.get(
@@ -64,6 +85,8 @@ function RouteComponent() {
6485
setIsLoading(true);
6586
const data = await fetchWrappedData(user.id);
6687
setWrappedData(data);
88+
const audioPromises = AUDIO_ASSETS.map(src => preloadAudio(src));
89+
await Promise.all(audioPromises);
6790
} catch (error) {
6891
console.error("Error fetching wrapped data:", error);
6992
} finally {

0 commit comments

Comments
 (0)