androidreact-nativeforeground-servicereact-native-track-player

react-native-track-player: Foreground service doesn't stop and audio keeps playing after app kill on android


For a better explanation check this issue. The issue has been closed with this fix, but the fix creates another issue which is still open.

react-native-track-player uses foreground service in android to display a notification on with buttons to play/pause audio. However, the issue I am facing is that when I kill the app, the service is killed only the first time. After that if I open the App again, and play an audio, it doesn't stop after app kill and the audio keeps playing.


Solution

  • The solution I've found is to reset the TrackPlayer on App unmount.

    Inside your top level component:

    useEffect(() =>
     {
       return () => {
         try { TrackPlayer.reset() } catch(){};
       };
     }, []);