-
Notifications
You must be signed in to change notification settings - Fork 7
/
playbackService.ts
41 lines (36 loc) · 1.51 KB
/
playbackService.ts
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
import { getDefaultStore } from 'jotai/vanilla'
import TrackPlayer, { Event } from 'react-native-track-player'
import { audioSleepTimeAtom } from '~features/bible/footer/atom'
export const PlaybackService = async function() {
TrackPlayer.addEventListener(Event.RemotePlay, () => TrackPlayer.play())
TrackPlayer.addEventListener(Event.RemotePause, () => TrackPlayer.pause())
TrackPlayer.addEventListener(Event.RemoteNext, () => TrackPlayer.skipToNext())
TrackPlayer.addEventListener(Event.RemotePrevious, () =>
TrackPlayer.skipToPrevious()
)
TrackPlayer.addEventListener(Event.RemoteJumpForward, async () => {
const progress = await TrackPlayer.getProgress()
const position = progress.position
return TrackPlayer.seekTo(position + 10)
})
TrackPlayer.addEventListener(Event.RemoteJumpBackward, async () => {
const progress = await TrackPlayer.getProgress()
const position = progress.position
TrackPlayer.seekTo(position - 10)
})
TrackPlayer.addEventListener(Event.RemoteSeek, async ({ position }) => {
TrackPlayer.seekTo(position)
})
TrackPlayer.addEventListener(Event.PlaybackProgressUpdated, async () => {
const defaultStore = getDefaultStore()
// Stop playback if sleep timer is set and has expired
if (defaultStore.get(audioSleepTimeAtom) > 0) {
const sleepTime = defaultStore.get(audioSleepTimeAtom)
const currentTime = Date.now()
const timeElapsed = currentTime - sleepTime
if (timeElapsed >= 0) {
TrackPlayer.pause()
}
}
})
}