I am making a simple game and I want to loop few songs when app is in foreground using MediaPlayer. Playback should stop when UI is not visible, so I decided to play it from activities and not a service.

The problem is that the app consists of multiple activities so I cant just pause it in every onPause, but I have figured that out.

What I cant figure out so far, is to when and where should I release MediaPlayer and other (SoundPool) resources, as there is no onDestroy with Applications as far as I know. Is there any way to figure out how many activities do I have left on the stack and when the last calls Activity.onDestroy(), release it there?

Thanks!

有帮助吗?

解决方案

Make a Service. Put your MediaPlayer and other resources in the Service. Bind each Activity to the Service in onStart as described in the Android docs and unbind in onStop. The Service will shut down when all components are unbound.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top