Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: timer sound issues #19715

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const TimerIndicator: React.FC<TimerIndicatorProps> = ({
const alreadyNotified = useRef<boolean>(false);
const [startTimerMutation] = useMutation(TIMER_START);
const [stopTimerMutation] = useMutation(TIMER_STOP);
const [songTrackState, setSongTrackState] = useState<string>(songTrack);

const startTimer = () => {
startTimerMutation();
Expand All @@ -57,9 +58,12 @@ const TimerIndicator: React.FC<TimerIndicatorProps> = ({
};

useEffect(() => {
alarm.current = new Audio(`${HOST}/resources/sounds/alarm.mp3`);
if (songTrackState !== songTrack) {
if (music.current) music.current.pause();
}
if (songTrack in trackName) {
music.current = new Audio(`${HOST}/resources/sounds/${trackName[songTrack]}.mp3`);
setSongTrackState(songTrack);
music.current.addEventListener('timeupdate', () => {
const buffer = 0.19;
// Start playing the music before it ends to make the loop gapless
Expand All @@ -76,6 +80,10 @@ const TimerIndicator: React.FC<TimerIndicatorProps> = ({
if (intervalRef.current) clearInterval(intervalRef.current);
if (music.current) music.current.pause();
};
}, [songTrack]);

useEffect(() => {
alarm.current = new Audio(`${HOST}/resources/sounds/alarm.mp3`);
}, []);

useEffect(() => {
Expand Down Expand Up @@ -120,6 +128,17 @@ const TimerIndicator: React.FC<TimerIndicatorProps> = ({
}
}, [time]);

useEffect(() => {
if (running && songTrack !== 'noTrack') {
if (music.current) music.current.play();
} else if (!running || songTrack === 'noTrack') {
if (music.current) music.current.pause();
}
if (running && alreadyNotified.current) {
alreadyNotified.current = false;
}
}, [running, songTrackState]);

useEffect(() => {
if (startedOn === 0) {
setTime(passedTime);
Expand Down