daily-examples/dailyjs/shared/hooks/useJoinSound.js

40 lines
957 B
JavaScript

import { useEffect, useCallback, useMemo } from 'react';
import { debounce } from 'debounce';
import { useCallState } from '../contexts/CallProvider';
import { useSound } from './useSound';
/**
* Convenience hook to play `join.mp3` when first other participants joins.
*/
export const useJoinSound = () => {
const { callObject } = useCallState();
const { load, play } = useSound('join.mp3');
useEffect(() => {
load();
}, [load]);
const debouncedPlay = useMemo(() => debounce(() => play(), 200), [play]);
useEffect(() => {
if (!callObject) return false;
const handleParticipantJoined = () => {
debouncedPlay();
};
callObject.on('participant-joined', handleParticipantJoined);
setTimeout(() => {
handleParticipantJoined();
}, 2000);
return () => {
callObject.off('participant-joined', handleParticipantJoined);
};
}, [callObject, debouncedPlay]);
};
export default useJoinSound;