How do I stop MediaPlayer only when whole app goes to background. If I call MediaPlayer.Stop() in onPause() it will be stopped in other activity.
Asked
Active
Viewed 170 times
-1
-
you can call in onDestroy() – Adil Jun 21 '18 at 06:29
-
https://developer.android.com/guide/components/activities/activity-lifecycle – Adil Jun 21 '18 at 06:29
-
check it https://stackoverflow.com/questions/18871288/android-media-player-plays-in-the-background-but-doesnt-stop-when-app-killed – Adil Jun 21 '18 at 06:31
-
Detect When app is going to background and stop the Mediaplayer. To detect app foreground and background state check this Link https://medium.com/@arturogdg/background-and-foreground-events-with-android-architecture-components-233fdd9fa855 – BalaramNayak Jun 21 '18 at 06:42
2 Answers
0
Sounds like you need to follow along these lines: Stop MediaPlayer service when app goes in Background. Switching to call it onDestroy() is another option.

Esperavo
- 3
- 3
0
ProcessLifecycleOwner solves this problem beautifully. It is a special kind of LifecycleOwner that is a composite of all of your Activities. It provides lifecycle for the whole application process.
You can use it to react on your app coming to the foreground or going to the background with very little code.
Checkout more details in this medium post React to foreground and background events with ProcessLifecycleOwner
Check demo at AppLifecycleDemo

Priyank Patel
- 12,244
- 8
- 65
- 85