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?
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.
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:
TrackPlayer.STATE_PLAYINGwill be6on Android,playingon iOS and3on UWP, those might change anytime, you shouldn't hardcode it.