Sound Service
The SoundService aims to replace the usage of react-native-sound.
Why?
Unfortunately some of the library's API is static, mainly Sound.setCategory; this is a problem because when used incorrectly it can affect how other sounds are heard in the device which you did not play. Furthermore, it differs between iOS and Android.
Since we are now starting to add sound assets, we need to be more careful with this usage.
How should I use it?
Two possible ways to use the SoundService are as follows:
playNotificationSound- probably play and forget
onPlayEnd = (result: OnPlayComplete) => {
if (result.type === 'success') {
// Do something
} else {
// Handle error
}
};
play = soundAsset => {
SoundService.playNotificationSound(soundAsset, this.onPlayEnd);
};
loadPlaybackSound- probably using async/await or a callback:
const player = useRef<SoundObject>(undefined);
const onLoadComplete = (result: SoundLoadResult) => {
if (result.type === 'error') {
// Handle error
} else {
if (!player.current) {
player.current = result.sound;
}
// Do something else
}
};
useEffect(() => {
SoundService.loadPlaybackSound(source, onLoadComplete);
// Or
// const result: SoundLoadResult = await SoundService.loadPlaybackSound(source);
}, []);
// Later on in some other place:
// player.current?.play?.(onComplete);