TrackPlayer.ProgressComponent does not return currect time when app is in background
Configuration
React Native Environment Info: System: OS: Windows 10 CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz Memory: 6.82 GB / 15.87 GB Binaries: Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: Version 3.4.0.0 AI-183.6156.11.34.5692245
"react-native-track-player": "^1.2.0"
Issue
I am using ProgressComponent to get track position and create a correct Slider for users, the problem is that when app is in background the ProgressComponent does not provide correct timing and instead, returns last valid numbers before app going to background mode. It is important to note that I need the position in order to determine when song is ended so that I load up next track
Code
My render is like this
render() {
if (this.prevProgress !== this.getProgress()) {
this.prevProgress = this.getProgress();
this.props.progress(Math.floor(this.getProgress() * this.props.maximum));
}
return (
<Slider
maximumValue={this.props.maximum}
minimumValue={0}
minimumTrackTintColor="#fff"
maximumTrackTintColor="grey"
thumbTintColor="#fff"
value={Math.floor(this.prevProgress * this.props.maximum)}
style={{
width: '90%',
}}
onSlidingComplete={this.props.seek}
/>
);
}
the render simply DOES NOT called in background so i used react-native-background-timer library
import BackgroundTimer from 'react-native-background-timer';
constructor(props) {
super(props);
this.prevProgress = 0.0;
BackgroundTimer.runBackgroundTimer(() => {
console.log(this.getProgress()); // this.state.position has same value too
},
1000);
}
componentWillUnmount(): void {
BackgroundTimer.stopBackgroundTimer();
}
which again will return same value throughout app in background state.
ProgressComponent is an UI utility. It won't update when the UI is not visible and you shouldn't be using it to do any player logic.
You should be using the playback service for that, which will keep running after the app is in background and it's the proper place to add this kind of logic.
yeah I gathered, I moved my functionality to service.js file and used TrackPlayer.addEventListener('playback-queue-ended' .