-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplayer-handler.js
58 lines (54 loc) · 1.38 KB
/
player-handler.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
/* @flow */
import { Alert } from 'react-native';
import TrackPlayer from 'react-native-track-player';
import { playbackState } from './src/redux/ducks/player';
import type { DispatchType } from './src/redux/types';
type Data = {
type: string,
position: number,
ducking: number,
error: string,
state: string,
};
async function playerHandler(dispatch: DispatchType, data: Data) {
switch (data.type) {
// Forward remote events to the player
case 'remote-play':
TrackPlayer.play();
break;
case 'remote-pause':
TrackPlayer.pause();
break;
case 'remote-stop':
TrackPlayer.stop();
break;
case 'remote-next':
TrackPlayer.skipToNext();
break;
case 'remote-previous':
TrackPlayer.skipToPrevious();
break;
case 'remote-seek':
TrackPlayer.seekTo(data.position);
break;
// You can make ducking smoother by adding a fade in/out
case 'remote-duck':
TrackPlayer.setVolume(data.ducking ? 0.5 : 1);
break;
// Playback updates
case 'playback-state': {
dispatch(playbackState(data.state));
break;
}
case 'playback-track-changed':
break;
case 'playback-error':
Alert.alert('An error occurred', data.error);
break;
default:
break;
}
}
export default function(dispatch: DispatchType) {
return playerHandler.bind(null, dispatch);
}