-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmManager.js
68 lines (59 loc) · 1.86 KB
/
AlarmManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useState } from 'react';
import { Vibration } from 'react-native';
import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av';
import CONSTANTS from './src/constants/Constants';
import { getAlarmSong, getUseVibration } from './src/utils/KeysManager';
export const AlarmManager = () => {
const [sound, setSound] = useState(null);
const [status, setStatus] = useState(null);
//Setup audio instance and load audio in. To be called during init.
const setupAudio = async () => {
await Audio.setAudioModeAsync({
staysActiveInBackground: true,
interruptionModeAndroid: InterruptionModeAndroid.DuckOthers,
interruptionModeIOS: InterruptionModeIOS.DuckOthers,
});
loadAudio();
};
const loadAudio = async () => {
let song = await getAlarmSong();
if (song == null) {
song = CONSTANTS.MUSIC.song1;
}
const { sound, status } = await Audio.Sound.createAsync(song.path);
setSound(sound);
setStatus(status);
await sound.setIsLoopingAsync(true);
};
const unloadAudio = async () => {
await sound?.unloadAsync();
setSound(null);
setStatus(null);
};
//Stops playing the alarm
const stopAlarm = async () => {
await sound?.stopAsync();
Vibration.cancel();
};
//Activates the alarm
const playAlarm = async () => {
if (sound == null) {
console.error('setupAudio() must be called');
return;
}
//BUG: isPlaying is always false
if (!status.isPlaying) {
await sound?.playAsync();
//Vibration
getUseVibration().then((vibration) => {
if (vibration) {
let VIBRATION_PATTERN = [200, 200];
let VIBRATION_REPEAT = true;
Vibration.vibrate(VIBRATION_PATTERN, VIBRATION_REPEAT);
}
});
}
};
return { setupAudio, loadAudio, stopAlarm, playAlarm, unloadAudio };
};
export default AlarmManager;