I think I found a bug - in iOS, TrackPlayer.STATE_PLAYING isn't exported into the module and it's being a null. The version is 0.2.2.
let state = await TrackPlayer.getState();
console.log('TrackPlayer', TrackPlayer.STATE_PLAYING); // This is 'null
if (state == TrackPlayer.STATE_PLAYING) {/*<= this is always false*/}
if (state == "STATE_PLAYING") {/*This works*/}
This doesn't happen in Android, only in iOS. Can this be fixed in the future release? This is not urgent at all b/c the workaround in the snippet does its job fine.
This is actually a better workaround:
if (state === "STATE_PLAYING" || state == 3) {}
In Android, TrackPlayer.getState() returns an integer whereas in iOS it's a string.
Do you have numbers for other states? Btw. this solution works like charm!
Here's the list for Android:
https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat.html
Check TrackModules.java in the package. The constants are from PlaybackStateCompat.
Found the same bug. On Android, TrackPlayer.STATE_PLAYING is a number, on iPhone, it is undefined.
There is definitely some inconsistency. For now, need to use my own constants as follows:
const STATE_PLAYING = Platform.OS === 'android' ? 3 : 'STATE_PLAYING';
const STATE_PAUSED = Platform.OS === 'android' ? 2 : 'STATE_PAUSED';
Here's a more complete snippet with custom constants defined so you don't have to go look them up:
import Platform from 'react-native'
const RnTrackPlayerConstants = {
STATE_NONE: Platform.OS === 'android' ? 0 : 'STATE_NONE',
STATE_STOPPED: Platform.OS === 'android' ? 1 : 'STATE_STOPPED',
STATE_PAUSED: Platform.OS === 'android' ? 2 : 'STATE_PAUSED',
STATE_PLAYING: Platform.OS === 'android' ? 3 : 'STATE_PLAYING',
STATE_BUFFERING: Platform.OS === 'android' ? 6 : 'STATE_BUFFERING',
}
@SatoshiInoue
your problem seems to be resolved with the latest version 0.2.3
I confirm that with version 0.2.2, all constants from TrackPlayer on iOS was empty, no problem with Android

@padupuy Finally! This inconsistency gave me a lot of bugs when I started to work with this package. Documentation didn't include the differences between Android and iOS.