1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
import React from "react";
import { IoPlay, IoPause } from "react-icons/io5";
import { playTimes } from "../../constants";
import * as Styled from "../YTPlayer/index.styled";
interface Props {
currentTry: number;
}
const MAX_TIME = 16;
const DEFAULT_VOLUME = 0.7;
const loadVolume = () => {
try {
const storedVolume = localStorage.getItem("playerVolume");
if (storedVolume === null) return DEFAULT_VOLUME;
const parsedVolume = Number(storedVolume);
if (!Number.isFinite(parsedVolume)) return DEFAULT_VOLUME;
return Math.max(0, Math.min(1, parsedVolume));
} catch {
return DEFAULT_VOLUME;
}
};
export function Player({ currentTry }: Props) {
const audioRef = React.useRef<HTMLAudioElement | null>(null);
const currentPlayTime = playTimes[currentTry];
const [play, setPlay] = React.useState(false);
const [currentTime, setCurrentTime] = React.useState(0);
const [isReady, setIsReady] = React.useState(false);
const [volume, setVolume] = React.useState(loadVolume);
const CDN_URL =
import.meta.env.VITE_CDN_URL || "localhost";
const dateString = new Date().toISOString().split("T")[0];
const startPlayback = React.useCallback(() => {
const audio = audioRef.current;
if (!audio) return;
audio.play();
setPlay(true);
}, []);
const stopPlayback = React.useCallback(() => {
const audio = audioRef.current;
if (!audio) return;
audio.pause();
audio.currentTime = 0;
setPlay(false);
}, []);
const updateVolume = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setVolume(Number(event.target.value));
},
[]
);
React.useEffect(() => {
const audio = new Audio(`${CDN_URL}/${dateString}.mp3`);
audio.volume = loadVolume();
audioRef.current = audio;
audio.addEventListener("loadeddata", () => {
setIsReady(true);
});
audio.addEventListener("timeupdate", () => {
setCurrentTime(audio.currentTime);
});
audio.addEventListener("ended", () => {
setPlay(false);
audio.currentTime = 0;
});
return () => {
audio.pause();
audio.src = "";
};
}, [CDN_URL, dateString]);
React.useEffect(() => {
if (!audioRef.current) return;
audioRef.current.volume = volume;
try {
localStorage.setItem("playerVolume", String(volume));
} catch {
}
}, [volume]);
React.useEffect(() => {
if (!play || !audioRef.current) return;
const interval = setInterval(() => {
const a = audioRef.current;
if (!a) return;
const t = a.currentTime * 1000;
setCurrentTime(a.currentTime);
if (t >= currentPlayTime || t >= MAX_TIME * 1000) {
a.pause();
a.currentTime = 0;
setPlay(false);
}
}, 100);
return () => clearInterval(interval);
}, [play, currentPlayTime]);
React.useEffect(() => {
if (!("mediaSession" in navigator)) return;
navigator.mediaSession.setActionHandler("play", () => undefined);
navigator.mediaSession.setActionHandler("pause", () => undefined);
navigator.mediaSession.setActionHandler("previoustrack", () => undefined);
navigator.mediaSession.setActionHandler("nexttrack", () => undefined);
return () => {
navigator.mediaSession.setActionHandler("play", null);
navigator.mediaSession.setActionHandler("pause", null);
navigator.mediaSession.setActionHandler("previoustrack", null);
navigator.mediaSession.setActionHandler("nexttrack", null);
};
}, []);
return (
<>
{isReady ? (
<>
<Styled.ProgressBackground>
{currentTime !== 0 && (
<Styled.Progress value={currentTime} />
)}
{playTimes.map((t) => (
<Styled.Separator
key={t}
style={{ left: `${(t / 16000) * 100}%` }}
/>
))}
</Styled.ProgressBackground>
<Styled.TimeStamps>
<Styled.TimeStamp>1s</Styled.TimeStamp>
<Styled.TimeStamp>16s</Styled.TimeStamp>
</Styled.TimeStamps>
{!play ? (
<IoPlay
style={{ cursor: "pointer" }}
size={36}
onClick={startPlayback}
/>
) : (
<IoPause
style={{ cursor: "pointer" }}
size={36}
onClick={stopPlayback}
/>
)}
<Styled.VolumeControl>
<Styled.VolumeLabel htmlFor="player-volume">
Volume {Math.round(volume * 100)}%
</Styled.VolumeLabel>
<Styled.VolumeSlider
id="player-volume"
type="range"
min="0"
max="1"
step="0.01"
value={volume}
onChange={updateVolume}
aria-label="Volume"
/>
</Styled.VolumeControl>
</>
) : (
<p>Loading audio...</p>
)}
</>
);
}
|