react-native-spotify-remote icon indicating copy to clipboard operation
react-native-spotify-remote copied to clipboard

Android: I want to pause when player when app terminate on android

Open IbrahimCanKALYA opened this issue 4 years ago • 4 comments

Is there any way to do it on android like ios?

IbrahimCanKALYA avatar Feb 01 '21 14:02 IbrahimCanKALYA

Good question, how are you doing it on iOS? I feel like it should be possible to do on android as well.

cjam avatar Feb 09 '21 14:02 cjam

@cjam #73 this solution works on iOS

IbrahimCanKALYA avatar Feb 09 '21 14:02 IbrahimCanKALYA

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.

cjam avatar Feb 14 '21 18:02 cjam

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...?🤔

nob3110 avatar Sep 10 '21 11:09 nob3110