Skip to content

Commit b3a7656

Browse files
Added a audioplayer context for maintaining state
1 parent 265a7b3 commit b3a7656

File tree

6 files changed

+132
-31
lines changed

6 files changed

+132
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// AudioPlayerContext.js
2+
import React, { createContext, useState, useRef, useEffect } from 'react';
3+
4+
const AudioPlayerContext = createContext();
5+
6+
export const AudioPlayerProvider = ({ children }) => {
7+
// Define your list of audio tracks.
8+
const audioList = [
9+
{
10+
src: "audio1.webm",
11+
title: "This is helpful while coding pt.1",
12+
description: "Lo-Fi लो-फाई (siddharth's playlist)"
13+
},
14+
{
15+
src: "audio2.webm",
16+
title: "This is helpful while coding pt.2",
17+
description: "Lo-Fi लो-फाई (siddharth's playlist)"
18+
}
19+
];
20+
21+
const [isPlaying, setIsPlaying] = useState(false);
22+
const [currentTrackIndex, setCurrentTrackIndex] = useState(0);
23+
const audioRef = useRef(null);
24+
25+
// Whenever the track index changes, update the audio source.
26+
useEffect(() => {
27+
if (audioRef.current) {
28+
audioRef.current.pause();
29+
}
30+
audioRef.current = new Audio(audioList[currentTrackIndex].src);
31+
// Optionally, resume playing if the previous track was playing.
32+
if (isPlaying) {
33+
audioRef.current.play();
34+
}
35+
}, [currentTrackIndex]);
36+
37+
const handlePlayPause = () => {
38+
if (!audioRef.current) return;
39+
if (isPlaying) {
40+
audioRef.current.pause();
41+
setIsPlaying(false);
42+
} else {
43+
// Optionally reset to start if needed:
44+
audioRef.current.currentTime = 0;
45+
audioRef.current.play();
46+
setIsPlaying(true);
47+
}
48+
};
49+
50+
const handleNext = () => {
51+
// Calculate the next track index and update the state.
52+
const nextIndex = (currentTrackIndex + 1) % audioList.length;
53+
setCurrentTrackIndex(nextIndex);
54+
// The useEffect hook will handle updating the audio source.
55+
};
56+
57+
return (
58+
<AudioPlayerContext.Provider
59+
value={{ isPlaying, handlePlayPause, handleNext, currentTrackIndex, audioList }}
60+
>
61+
{children}
62+
</AudioPlayerContext.Provider>
63+
);
64+
};
65+
66+
export default AudioPlayerContext;

app/routes/components/ControlCenter.jsx

+39-30
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import AudioPlayIcon from "../icons/AudioPlayIcon"
1111
import AudioNextIcon from "../icons/AudioNextIcon"
1212
import AudioPauseIcon from "../icons/AudioPauseIcon"
1313
import { FullScreenContext } from "./FullScreenContext";
14+
import AudioPlayerContext from './AudioPlayerContext';
15+
import audioManager from '../components/audioManager'
1416
import "../styles/ControlCenter.scss"
1517
import { useState, useRef, useContext, useEffect} from "react"
16-
import React from "react"
18+
1719
export default function ControlCenter(){
1820

1921
const connectivity =[
@@ -22,25 +24,27 @@ const connectivity =[
2224
{id:3, Icon1: <AirDropIcon />, textbig: "Airdrop",textsmall: "Contacts Only" },
2325
]
2426

25-
const audioList = [
26-
{
27-
src: "audio1.mp3",
28-
title: "This is helpful while coding pt.1",
29-
description: "Lo-Fi लो-फाई (siddharth's playlist)"
30-
},
31-
{
32-
src: "audio2.mp3",
33-
title: "This is helpful while coding pt.2",
34-
description: "Lo-Fi लो-फाई (siddharth's playlist)"
35-
}
36-
];
37-
const [currentTrackIndex, setCurrentTrackIndex] = useState(0);
38-
const [isPlaying, setIsPlaying] = useState(false);
27+
// const audioList = [
28+
// {
29+
// src: "audio1.webm",
30+
// title: "This is helpful while coding pt.1",
31+
// description: "Lo-Fi लो-फाई (siddharth's playlist)"
32+
// },
33+
// {
34+
// src: "audio2.webm",
35+
// title: "This is helpful while coding pt.2",
36+
// description: "Lo-Fi लो-फाई (siddharth's playlist)"
37+
// }
38+
// ];
39+
// const [currentTrackIndex, setCurrentTrackIndex] = useState(0);
40+
// const [isPlaying, setIsPlaying] = useState(false);
3941
const [volume, setVolume] = useState(70);
4042
const audioRef = useRef(null);
4143
const [selectedItems, setSelectedItems] = useState({});
4244
const [brightness, setBrightness] = useState(100);
4345
const {fullScreenSelected, toggleFullScreen } = useContext(FullScreenContext);
46+
const { isPlaying, handlePlayPause, handleNext, currentTrackIndex, audioList } = useContext(AudioPlayerContext);
47+
4448

4549
const handleVolumeChange = (e) => {
4650
const newVolume = e.target.value;
@@ -50,22 +54,27 @@ const handleVolumeChange = (e) => {
5054
}
5155
};
5256

53-
const handlePlayPause = () => {
54-
if (audioRef.current) {
55-
if (isPlaying) {
56-
audioRef.current.pause();
57-
} else {
58-
audioRef.current.currentTime = 0;
59-
audioRef.current.play();
60-
}
61-
setIsPlaying(!isPlaying);
62-
}
63-
};
57+
// const handlePlayPause = () => {
58+
// if (audioRef.current) {
59+
// if (isPlaying) {
60+
// audioManager.pause();
61+
// } else {
62+
// // audioRef.current.currentTime = 0;
63+
// // audioRef.current.play();
64+
// audioManager.play(audioList[currentTrackIndex].src);
65+
// }
66+
// setIsPlaying(!isPlaying);
67+
// }
68+
// };
6469

65-
const handleNext = () => {
66-
setCurrentTrackIndex((prevIndex) => (prevIndex + 1) % audioList.length);
67-
setIsPlaying(false);
68-
};
70+
// const handleNext = () => {
71+
// // setCurrentTrackIndex((prevIndex) => (prevIndex + 1) % audioList.length);
72+
// // setIsPlaying(false);
73+
// const nextIndex = (currentTrackIndex + 1) % audioList.length;
74+
// setCurrentTrackIndex(nextIndex);
75+
// audioManager.play(audioList[nextIndex].src);
76+
// setIsPlaying(true);
77+
// };
6978

7079
const handleLofiWebsite = () =>{
7180
window.open("https://lofigirl.com/releases/sleeping-soul/", "");

app/routes/components/audioManager.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class AudioManager {
2+
constructor() {
3+
this.audio = null;
4+
}
5+
6+
play(src) {
7+
if (this.audio) {
8+
this.audio.pause();
9+
}
10+
this.audio = new Audio(src);
11+
this.audio.play();
12+
}
13+
14+
pause() {
15+
if (this.audio) {
16+
this.audio.pause();
17+
}
18+
}
19+
20+
}
21+
22+
export default new AudioManager();
23+

app/routes/homepage.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import WifiIcon from './icons/WifiIcon'
1818
import ControlCenterIcon from './icons/ControlCenterIcon'
1919
import React from "react";
2020
import { FullScreenProvider } from "./components/FullScreenContext";
21+
import { AudioPlayerProvider } from './components/AudioPlayerContext';
2122

2223
export default function homepage(){
2324
const location = useLocation();
@@ -248,6 +249,7 @@ useEffect(() => {
248249

249250

250251
return(
252+
<AudioPlayerProvider>
251253
<FullScreenProvider>
252254
<div className="homepage-container">
253255
<img
@@ -426,6 +428,7 @@ useEffect(() => {
426428
))}
427429
</div>
428430
</div>
429-
</FullScreenProvider>
431+
</FullScreenProvider>
432+
</AudioPlayerProvider>
430433
);
431434
}

public/audio1.webm

2.43 MB
Binary file not shown.

public/audio2.webm

2.25 MB
Binary file not shown.

0 commit comments

Comments
 (0)