react-native-spotify-remote
react-native-spotify-remote copied to clipboard
Android: I want to pause when player when app terminate on android
Is there any way to do it on android like ios?
Good question, how are you doing it on iOS? I feel like it should be possible to do on android as well.
@cjam #73 this solution works on iOS
Right, yea I think if you could pull the current module from the react context in android. You should be able to call any of the methods on it (similar to on iOS). On iOS we use a singleton pattern and make the instance accessible statically. But on Android, I believe you can reach into the React context and pull the native module out.
I did a quick search and found this stack overflow which might be helpful:
https://stackoverflow.com/questions/35278646/react-native-android-native-module-get-access-to-reactcontext-in-another-clas
Either way, I think there should be a way to get the current instance from the runtime in android. Just haven't tried doing it myself.
I scrutinized how to achieve this. In conclusion, there seems to be no way to do it with React Native or native code.
an example of stack overflow. android - How to know when my app has been killed? - Stack Overflow
There is an idea to launch a service, but it can't be used on Android O and above.
NOTE: In Android O + this solution only works when the app is full-time in foreground.
Here is the approach I made. However, this is not perfect ...😓
- Monitor the online status of your app using the Firebase Realtime Database onDisconnect method
- Stop Spotify playback in Firebase Cloud Functions when the app goes offline
👍Good Point: Works on iOS too. 💩Bad Point: Unintentional stop (when the device sleeps or the app goes to the background, the offline signal occasionally fires and playback stops)
Anyone else have a good idea...?🤔