React-native-track-player: State returns a number in android and string in ios

Created on 14 Aug 2018  路  1Comment  路  Source: react-native-kit/react-native-track-player

Hi,
I have this event handler :

   public async componentDidMount() {
    const handler = async (data) => {
      console.log(data);
      if (data.type === 'playback-state') {
        if (Platform.OS === 'ios') {
          this.setState({ playerState: data.state });
        } else {
          this.setState({ playerState: androidStates[data.state] });
        }
        // Update the UI with the new state
      } else if (data.type === 'remote-play') {
        // The play button was pressed, we can forward this command to the player using
        TrackPlayer.play();
      } else if (data.type === 'remote-stop') {
        // The stop button was pressed, we can stop the player
        TrackPlayer.stop();
      } else if (data.type === 'remote-pause') {
        // The play button was pressed, we can forward this command to the player using
        TrackPlayer.pause();
      }
    };

The console log in the 3rd line returns this for Android:
{type: "playback-state", state: 6}

And this for iOs:
{type: "playback-state", state: "playing"}

Any reason?

Question

Most helpful comment

It's just how the state is internally on each platform. As javascript is not an object oriented language, this is not a problem.

You should store the state as is and use our constants to check it:

if(state == TrackPlayer.STATE_PLAYING) {
    // ...
}

TrackPlayer.STATE_PLAYING will be 6 on Android, playing on iOS and 3 on UWP, those might change anytime, you shouldn't hardcode it.

>All comments

It's just how the state is internally on each platform. As javascript is not an object oriented language, this is not a problem.

You should store the state as is and use our constants to check it:

if(state == TrackPlayer.STATE_PLAYING) {
    // ...
}

TrackPlayer.STATE_PLAYING will be 6 on Android, playing on iOS and 3 on UWP, those might change anytime, you shouldn't hardcode it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

toooldmohammad picture toooldmohammad  路  3Comments

mnlbox picture mnlbox  路  4Comments

tarahiw picture tarahiw  路  3Comments

moduval picture moduval  路  3Comments

krunalbad picture krunalbad  路  3Comments