Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
if (data.state == TrackPlayer.STATE_PAUSED) {
console.log('STATE_PAUSED');
}
if (data.state == TrackPlayer.STATE_STOPPED) {
console.log('STATE_STOPPED');
}
if (data.state == TrackPlayer.STATE_BUFFERING) {
console.log('STATE_BUFFERING');
_fetchURLs(false, 1, 1, () => {}, () => {});
}
} else if (data.type == 'remote-play') {
TrackPlayer.play();
} else if (data.type == 'remote-pause') {
TrackPlayer.pause();
} else if (data.type == 'remote-next') {
TrackPlayer.skipToNext();
} else if (data.type == 'remote-previous') {
TrackPlayer.skipToPrevious();
} else if (data.type == 'remote-seek') {
TrackPlayer.seekTo(data.position);
} else if (data.type == 'remote-duck') {
if (data.paused) TrackPlayer.pause();
if (data.permanent) TrackPlayer.stop();
if (data.ducking) {
const prevVolume = await TrackPlayer.getVolume();
TrackPlayer.setVolume(0.1);
BackgroundTimer.setTimeout(() => {
TrackPlayer.setVolume(prevVolume);
}, 3 * 1000);
}
}
async function Handler(dispatch) {
TrackPlayer.addEventListener('remote-play', () =>
dispatch(setUserPlaying(true))
)
TrackPlayer.addEventListener('remote-pause', () =>
dispatch(setUserPlaying(false))
)
TrackPlayer.addEventListener('remote-stop', () => {
dispatch(setUserPlaying(false))
TrackPlayer.stop()
})
TrackPlayer.addEventListener('remote-next', () => TrackPlayer.skipToNext())
TrackPlayer.addEventListener('remote-previous', async () => {
const time = await TrackPlayer.getPosition()
if (time <= 3) {
TrackPlayer.skipToPrevious()
} else {
TrackPlayer.seekTo(0)
}
})
TrackPlayer.addEventListener('remote-seek', ({ position }) =>
TrackPlayer.seekTo(position)
)
if (Platform.OS !== 'ios') {
const onPressNext = () => {
TrackPlayer.skipToNext()
}
async function skipToNext() {
try {
await TrackPlayer.skipToNext();
} catch (_) {}
}
async function Handler(dispatch, data) {
switch (data.type) {
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
case 'remote-duck':
TrackPlayer.setVolume(data.ducking ? 0.5 : 1)
break
case 'playback-state':
dispatch(playbackState(data.state))
break
case 'playback-track-changed':
dispatch(playbackTrack(data.nextTrack))
TrackPlayer.addEventListener('remote-next', () => {
TrackPlayer.skipToNext()
});
const skipToNext = async () => {
try {
await TrackPlayer.skipToNext();
} catch (_) {}
};