Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement a media player control as a footer persistent across activities

Tags:

android

I am working on a music player app. One of the requirements I have is to display a media control bar at the bottom of the screen (with progress indicator and a stop button). This bar has to persist across the whole application while the music is playing in the background even when the user navigates between activities.

I would like some advice on how to best implement this, the key challenge for me being to find a solution to keep this control bar visible across all activities, 'floating' above the screens when the music is playing.

I am currently playing the music via an android Service using a MediaPlayer so the bar has to sync its state with the service.

After doing some research, I've found a few hints on what I could do, but none seem ideal:

  • using android notifications. Not an option for me since I need the bar to be at the bottom of the screen.
  • Make my whole application be a single activity and use fragments heavily. The control bar would just be a footer view of the activity that syncs with the service (via a local BroadcastReceiver or other callback methods). All my current activities would be converted to fragments inside the 'shell' activity and I'll need to manage the fragment backstack. I just don't like this option, it seems messy and a lot of re-factoring work. Couple of cons I can see already is that I won't get any more any useful debugging information when I run 'adb shell dumpsys activity' and look at running activities. Also not sure if I'll be able to use fragments in case I need to share components between screens..(I haven't checked I can use a fragment inside a fragment's layout, but I doubt it).

One option I thought of, but it's not elegant, is for this control bar to be a fragment that I'll include in all my activities. When the activity starts, bind to the service to get the latest state and then use callback mechanism to get regular updates (via BroadcastReceiver).

Any better/other ideas?

Thanks.

like image 238
user751441 Avatar asked Nov 10 '22 14:11

user751441


1 Answers

Most non-intrusive way is the one you described. Or check:

How to implement a popup overlay that can be displayed over any other app in Android

like image 96
Kamen Avatar answered Nov 14 '22 21:11

Kamen