Add calling and notification sounds (#119)

* Add calling and notification sounds

* Fix notification visibility on any resolution

* Add comments

* Log howler error src

* Revert removed files im stupid

* Make sliders on settings use changeCommitted instead of onChange

* Fix ringtone paths

* Break alert words overflow

* Fix tweet overflow pepega

* Remove unused hook

* Update volume sliders in real time

* Fix resolution notifications

* Fix initial value for vol sliders

* Fix call duration timer bugs

* Add alert when phone active and phone closes

* keep notification

* Stop ringing when accept
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-03-01 17:22:58 -05:00
1 parent 8ecb91e71f
commit 4eda44b9c2
28 files changed
+589 -142

No files matched your search

@@ -0,0 +1,47 @@
import { useCallback, useContext, useEffect, useMemo } from 'react';
import { useSettings } from '../../../apps/settings/hooks/useSettings';
import { soundContext } from '../providers/SoundProvider';
import { getSoundSettings } from '../utils/getSoundSettings';
export const useRingtoneSound = () => {
const [settings] = useSettings();
const context = useContext(soundContext);
if (!context) {
throw new Error('useRingtoneSound must be wrapped in SoundProvider');
}
const options = useMemo(() => getSoundSettings('ringtone', settings), [
settings,
]);
useEffect(() => {
if (!context.isMounted(options.sound)) {
context.mount(options.sound, options.volume, true);
return;
}
context.volume(options.sound, options.volume);
}, [context, options.sound, options.volume]);
const play = useCallback(() => {
context.play(options.sound, options.volume, true);
}, [context, options.sound, options.volume]);
const stop = useCallback(() => {
context.stop(options.sound);
}, [context, options.sound]);
const playing = useCallback(() => {
return context.playing(options.sound);
}, [context, options.sound]);
return useMemo(
() => ({
play,
stop,
playing,
}),
[play, playing, stop]
);
};
+89
View File
@@ -0,0 +1,89 @@
import { useState, useEffect, useRef, useCallback } from 'react';
import { Howl } from 'howler';
interface ISoundOptions {
volume?: number;
interrupt?: boolean;
loop?: boolean;
}
/**
* A hook allowing to play sound
* @param url The url of the sound you would like to play
*
* @param { volume, interrupt, loop} Additional options
**/
const useSound = (
url: string,
{ volume = 1, interrupt = false, loop = false }: ISoundOptions = {}
) => {
const [isMounted, setMounted] = useState<boolean>(false);
const soundRef = useRef<Howl>(null);
const [playing, setPlaying] = useState<boolean>(false);
// Mount & Dismount handling
useEffect(() => {
soundRef.current?.unload();
setPlaying(false);
setMounted(false);
soundRef.current = new Howl({
src: url,
volume: 1,
loop: false,
});
setMounted(true);
return () => {
soundRef.current?.unload();
soundRef.current = undefined;
};
}, [url]);
useEffect(() => {
if (isMounted) {
soundRef.current.volume(volume);
}
}, [volume, isMounted]);
useEffect(() => {
if (isMounted) {
soundRef.current.loop(loop);
}
}, [loop, isMounted]);
const play = useCallback(() => {
if (!soundRef.current) return;
if (isMounted) {
soundRef.current.play();
soundRef.current.once('end', () => {
if (!soundRef.current.playing()) {
setPlaying(false);
}
});
setPlaying(true);
}
}, [isMounted]);
const stop = useCallback(() => {
if (!soundRef.current) return;
if (isMounted) {
soundRef.current.stop();
setPlaying(false);
}
}, [isMounted]);
useEffect(() => {
if (interrupt && isMounted) {
stop();
}
}, [interrupt, isMounted, stop]);
return { play, playing, stop };
};
export default useSound;
@@ -0,0 +1,154 @@
import { Howl } from 'howler';
import React, { createContext, useCallback, useEffect, useRef } from 'react';
interface IMountResponse {
howl: Howl;
url: string;
}
interface ISoundContext {
mount(
url: string,
volume?: number,
loop?: boolean,
autoplay?: boolean
): Promise<IMountResponse>;
play(url?: string, volume?: number, loop?: boolean): void;
stop(url: string): void;
remove(url: string): boolean;
volume(url: string, volume?: number): number;
playing(url: string): boolean;
isMounted(url: string): boolean;
}
export const soundContext = createContext<ISoundContext>(null);
export const SoundProvider = ({ children }: { children: React.ReactNode }) => {
const soundRefs = useRef<Map<string, { howl: Howl; volume: number }>>();
useEffect(() => {
soundRefs.current = new Map();
}, []);
const mount = (
url: string,
volume: number = 1,
loop: boolean = false,
autoplay: boolean = false
) => {
return new Promise<IMountResponse>((res) => {
if (!soundRefs.current) {
soundRefs.current = new Map();
}
const find = soundRefs.current.get(url);
if (find) {
find.howl.volume(volume);
find.howl.loop(loop);
if (autoplay) {
find.howl.play();
}
return res({ howl: find.howl, url });
}
const instance = new Howl({
src: url,
loop,
volume,
autoplay,
});
soundRefs.current.set(url, { howl: instance, volume });
instance.once('load', () => {
console.log('ready', instance);
res({ howl: instance, url });
});
const onError = (_id, e) => {
res(null);
console.error('Howler Error \n', e, '\nid: ', _id, '\nsrc: ', url);
};
instance.once('loaderror', onError);
instance.once('playerror', onError);
});
};
const play = useCallback((url: string, volume: number, loop: boolean) => {
const sound = soundRefs.current.get(url);
let fadedSounds = [];
if (sound) {
soundRefs.current.forEach((ref, key) => {
const isCurrent = key === url;
const isPlaying = ref.howl.playing();
const isLoop = ref.howl.loop();
if (isCurrent || !isLoop || !isPlaying) {
return;
}
fadedSounds.push(ref);
const currVol = ref.howl.volume();
ref.howl.fade(currVol, currVol * 0.3, 50);
});
if (volume !== undefined) {
sound.howl.volume(volume);
}
if (loop !== undefined) {
sound.howl.loop(loop);
}
sound.howl.play();
sound.howl.once('end', () => {
fadedSounds.forEach((s) => {
s.howl.fade(s.howl.volume(), s.volume, 50);
});
fadedSounds = undefined;
});
return;
}
mount(url, volume, loop, true);
}, []);
const volume = (url: string, volume: number = undefined): number => {
const sound = soundRefs.current.get(url);
return sound?.howl.volume(volume) as number;
};
const stop = (url: string) => {
const sound = soundRefs.current.get(url);
sound?.howl.stop();
};
const remove = (url: string): boolean => {
const sound = soundRefs.current.get(url);
if (sound) {
sound.howl.stop();
sound.howl.unload();
soundRefs.current.delete(url);
return true;
}
return false;
};
const isMounted = (url: string): boolean => {
if (!soundRefs.current) {
return false;
}
return !!soundRefs.current.get(url);
};
const playing = (url: string) => {
const sound = soundRefs.current.get(url);
return sound?.howl.playing() || false;
};
return (
<soundContext.Provider
value={{ mount, play, stop, playing, remove, volume, isMounted }}
>
{children}
</soundContext.Provider>
);
};
@@ -0,0 +1,34 @@
import { IPhoneSettings } from '../../../apps/settings/hooks/useSettings';
import {
getNotificationPath,
getRingtonePath,
} from '../../../apps/settings/utils/getRingtonePath';
const getPath = {
ringtone: getRingtonePath,
notification: getNotificationPath,
};
/**
* Gets the configuration for SoundProvider based on settings and apps
*/
export const getSoundSettings = (
type: 'ringtone' | 'notification',
settings: IPhoneSettings,
app?: string
) => {
try {
return app
? {
sound: getPath[type](settings[`${app}_${type}`]),
volume: settings[`${app}_${type}Vol`] / 100,
}
: {
sound: getPath[type](settings[type]),
volume: settings[`${type}Vol`] / 100,
};
} catch (e) {
console.error('getSoundSettings.ts error', e, 'Using default sounds');
return { sound: getPath[type](), volume: 1 };
}
};