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

wip: poc for playing a sound on time end #1245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 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);
}
}
};
cpvalente marked this conversation as resolved.
Show resolved Hide resolved

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();
cpvalente marked this conversation as resolved.
Show resolved Hide resolved

useWindowTitle('Timer');

Expand Down