Created
July 29, 2020 15:11
-
-
Save ShivamJoker/2e147b7ec74cd648919643629e26d29a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useRef, useEffect, useState} from 'react'; | |
import { | |
View, | |
SafeAreaView, | |
Text, | |
Image, | |
FlatList, | |
Dimensions, | |
Animated, | |
StyleSheet, | |
} from 'react-native'; | |
import TrackPlayer, { | |
Capability, | |
usePlaybackState, | |
useProgress, | |
Event, | |
} from 'react-native-track-player'; | |
import songs from './data'; | |
import Controller from './Controller'; | |
import SliderComp from './SliderComp'; | |
const {width, height} = Dimensions.get('window'); | |
export default function PlayerScreen() { | |
const scrollX = useRef(new Animated.Value(0)).current; | |
const scrollXVal = useRef(null); | |
const slider = useRef(null); | |
const isPlayerReady = useRef(false); | |
const [songIndex, setSongIndex] = useState(0); | |
const isAutomatic = useRef(true); | |
// for tranlating the album art | |
const position = useRef(Animated.divide(scrollX, width)).current; | |
const playbackState = usePlaybackState(); | |
const progress = useProgress(); | |
useEffect(() => { | |
// position.addListener(({ value }) => { | |
// console.log(value); | |
// }); | |
scrollX.addListener(({value}) => { | |
const index = Math.round(value / width); | |
setSongIndex(index); | |
scrollXVal.current = value; | |
}); | |
TrackPlayer.setupPlayer().then(async () => { | |
// The player is ready to be used | |
console.log('Player ready'); | |
// add the array of songs in the playlist | |
await TrackPlayer.add(songs); | |
TrackPlayer.play(); | |
isPlayerReady.current = true; | |
await TrackPlayer.updateOptions({ | |
stopWithApp: false, | |
capabilities: [ | |
Capability.Play, | |
Capability.Pause, | |
Capability.SkipToNext, | |
Capability.SkipToPrevious, | |
], | |
}); | |
//add listener on track change | |
TrackPlayer.addEventListener(Event.PlaybackTrackChanged, async (e) => { | |
let position = Math.round(e.position); | |
let duration = Math.round(await TrackPlayer.getDuration()); | |
console.log('position ', position, 'duration', duration); | |
if (position === duration) { | |
console.log('Auto ended'); | |
isAutomatic.current = true; | |
setSongIndex(songIndex + 1); | |
goNext(); | |
setTimeout(() => { | |
isAutomatic.current = false; | |
}, 300); | |
} | |
// isPlayerReady.current = true; | |
}); | |
}); | |
return () => { | |
scrollX.removeAllListeners(); | |
// TrackPlayer.destroy(); | |
TrackPlayer.reset() | |
exitPlayer(); | |
}; | |
}, []); | |
// change the song when index changes | |
useEffect(() => { | |
console.log('song index', songIndex); | |
if (isPlayerReady.current && !isAutomatic.current) { | |
TrackPlayer.skip(songs[songIndex].id) | |
.then((_) => { | |
console.log('changed track'); | |
}) | |
.catch((e) => console.log('error in changing track ', e)); | |
} | |
}, [songIndex]); | |
const exitPlayer = async () => { | |
try { | |
await TrackPlayer.stop(); | |
} catch (error) { | |
console.error('exitPlayer', error); | |
} | |
}; | |
const goNext = () => { | |
console.log(scrollXVal); | |
slider.current.scrollToOffset({ | |
offset: scrollXVal.current + width, | |
}); | |
}; | |
const goPrv = () => { | |
slider.current.scrollToOffset({ | |
offset: scrollXVal.current - width, | |
}); | |
}; | |
const renderItem = ({index, item}) => { | |
return ( | |
<Animated.View | |
style={{ | |
alignItems: 'center', | |
width: width, | |
transform: [ | |
{ | |
translateX: Animated.multiply( | |
Animated.add(position, -index), | |
-100, | |
), | |
}, | |
], | |
}}> | |
<Animated.Image | |
source={item.artwork} | |
style={{width: 320, height: 320, borderRadius: 5}} | |
/> | |
</Animated.View> | |
); | |
}; | |
return ( | |
<SafeAreaView style={styles.container}> | |
<SafeAreaView style={{height: 320}}> | |
<Animated.FlatList | |
ref={slider} | |
horizontal | |
pagingEnabled | |
showsHorizontalScrollIndicator={false} | |
scrollEventThrottle={16} | |
data={songs} | |
renderItem={renderItem} | |
keyExtractor={(item) => item.id} | |
onScroll={Animated.event( | |
[{nativeEvent: {contentOffset: {x: scrollX}}}], | |
{useNativeDriver: true}, | |
)} | |
/> | |
</SafeAreaView> | |
<View> | |
<Text style={styles.title}>{songs[songIndex].title}</Text> | |
<Text style={styles.artist}>{songs[songIndex].artist}</Text> | |
</View> | |
<SliderComp /> | |
<Controller onNext={goNext} onPrv={goPrv} /> | |
</SafeAreaView> | |
); | |
} | |
const styles = StyleSheet.create({ | |
title: { | |
fontSize: 28, | |
textAlign: 'center', | |
fontWeight: '600', | |
textTransform: 'capitalize', | |
color: '#ffffff', | |
}, | |
artist: { | |
fontSize: 18, | |
textAlign: 'center', | |
color: '#ffffff', | |
textTransform: 'capitalize', | |
}, | |
container: { | |
justifyContent: 'space-evenly', | |
alignItems: 'center', | |
height: height, | |
maxHeight: 600, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment