react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

TrackPlayer.ProgressComponent does not return currect time when app is in background

Open DNA-h opened this issue 7 years ago • 2 comments

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.

DNA-h avatar Oct 17 '19 14:10 DNA-h

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.

Guichaguri avatar Dec 27 '19 16:12 Guichaguri

yeah I gathered, I moved my functionality to service.js file and used TrackPlayer.addEventListener('playback-queue-ended' .

DNA-h avatar Jan 06 '20 13:01 DNA-h