Skip to content

Commit

Permalink
wip: poc for playing a sound on time end
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Oct 7, 2024
1 parent 86e6f8b commit d02940b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Binary file added apps/client/src/assets/sounds/buzzer.mp3
Binary file not shown.
8 changes: 8 additions & 0 deletions apps/client/src/common/hooks/useSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ export const useTimer = () => {
return useRuntimeStore(featureSelector);
};

export const useTimerPhase = () => {
const featureSelector = (state: RuntimeStore) => ({
phase: state.timer.phase,
});

return useRuntimeStore(featureSelector);
};

export const useClock = () => {
const featureSelector = (state: RuntimeStore) => ({
clock: state.clock,
Expand Down
21 changes: 21 additions & 0 deletions apps/client/src/features/viewers/timer/Timer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';
import { usePrevious } from '@mantine/hooks';
import { AnimatePresence, motion } from 'framer-motion';
import {
CustomFields,
Expand All @@ -12,12 +14,14 @@ import {
ViewSettings,
} from 'ontime-types';

import sound from '../../../assets/sounds/buzzer.mp3';
import { overrideStylesURL } from '../../../common/api/constants';
import { FitText } from '../../../common/components/fit-text/FitText';
import MultiPartProgressBar from '../../../common/components/multi-part-progress-bar/MultiPartProgressBar';
import TitleCard from '../../../common/components/title-card/TitleCard';
import ViewParamsEditor from '../../../common/components/view-params-editor/ViewParamsEditor';
import { useRuntimeStylesheet } from '../../../common/hooks/useRuntimeStylesheet';
import { useTimerPhase } from '../../../common/hooks/useSocket';
import { useWindowTitle } from '../../../common/hooks/useWindowTitle';
import { ViewExtendedTimer } from '../../../common/models/TimeManager.type';
import { formatTime, getDefaultFormat } from '../../../common/utils/time';
Expand Down Expand Up @@ -59,12 +63,28 @@ interface TimerProps {
viewSettings: ViewSettings;
}

const usePhaseEvent = () => {
const { phase } = useTimerPhase();
const previousValue = usePrevious(phase);

const audio = useMemo(() => new Audio(sound), []);

if (previousValue !== TimerPhase.None && previousValue !== phase && phase === TimerPhase.Overtime) {
try {
audio.play();
} catch (error) {
console.error('Audio playback prevented', error);
}
}
};

export default function Timer(props: TimerProps) {
const { auxTimer, customFields, eventNow, eventNext, isMirrored, message, settings, time, viewSettings } = props;

const { shouldRender } = useRuntimeStylesheet(viewSettings?.overrideStyles && overrideStylesURL);
const { getLocalizedString } = useTranslation();
const [searchParams] = useSearchParams();
usePhaseEvent();

useWindowTitle('Timer');

Expand Down Expand Up @@ -172,6 +192,7 @@ export default function Timer(props: TimerProps) {
return (
<div className={showFinished ? `${baseClasses} stage-timer--finished` : baseClasses} data-testid='timer-view'>
<ViewParamsEditor viewOptions={timerOptions} />
<audio controls src='../../assets/sounds/buzzer.mp3' />
<div className={message.timer.blackout ? 'blackout blackout--active' : 'blackout'} />
{!userOptions.hideMessage && (
<div className={showOverlay ? 'message-overlay message-overlay--active' : 'message-overlay'}>
Expand Down

0 comments on commit d02940b

Please sign in to comment.