Creating a persistent media control bar across multiple activities in an Android app

I’m working on an Android app that plays videos from YouTube and other streaming sources. The app has several activities and fragments. I want to add a small media control bar at the bottom of every screen when media is playing, even if the user navigates away from the main player screen.

This control bar should work like the ones in Spotify or Google Play Music. It needs to stay visible across all activities and fragments in the app.

I’ve looked at a few solutions, but they don’t quite fit my needs:

  1. Using a single activity with multiple fragments. This won’t work for my app structure.
  2. Collapsing bottom bar examples. These don’t address persistence across multiple screens.
  3. The AndroidSlidingUpPanel library. I’m not sure if this can handle persistence throughout the app.

Has anyone implemented something similar? How can I create this persistent control bar that works across multiple activities and fragments? Any tips or code examples would be really helpful.

I’m aiming for something that looks like this:

[A small media control bar at the bottom of the screen showing song/video info and play/pause controls]

Thanks for any advice!

Having implemented a similar feature, I’d suggest using a combination of a custom View and a bound Service. Create a MediaControlView and add it to the layout of each Activity. Implement a Service to handle media playback and communicate with Activities via binding.

Use a singleton pattern to maintain the global state of your player. This ensures consistency across different screens. Implement proper state saving and restoration to handle configuration changes and process death.

One often overlooked aspect is handling audio focus. Make sure your app responds appropriately to audio focus changes for a seamless user experience.

For smooth transitions, consider using shared element transitions when moving between the main player and other screens. This creates a visual connection that enhances the user experience.

Remember to test thoroughly, especially for edge cases like rapid activity switching or incoming calls during playback.

Having implemented a similar feature, I can share some insights. A viable approach is to use a custom Application class combined with a singleton pattern. This allows you to maintain a global state for your media player across all activities.

In the Application class, create a MediaPlayer instance and methods to control playback. Then, in each activity’s onCreate(), inflate your custom media control view and update it based on the global state.

To handle navigation, use a BroadcastReceiver in each activity to listen for playback changes. This way, when a user switches activities, the new activity can immediately reflect the current playback state.

For persistence, save playback information to SharedPreferences when the app is paused or stopped. Restore this state when the app resumes.

This approach has worked well in my experience, providing a seamless media control experience across multiple activities without major restructuring of the app architecture.

I’ve actually tackled a similar challenge in one of my recent projects. What worked well for me was using a combination of a custom View and a Service.

First, I created a custom View for the media control bar. Then, I implemented a foreground Service to manage the media playback and update the control bar state.

To make it persistent across activities, I added the custom View to each activity’s layout. The Service communicates with the activities through a bound connection, updating the control bar as needed.

One tricky part was handling configuration changes and process death. I used savedInstanceState to preserve the playback state and position.

For a polished look, I implemented smooth animations when showing/hiding the control bar. Also, I made sure to handle audio focus properly.

It took some trial and error, but the end result was pretty solid. The key is to keep the actual media logic separate from the UI components. This approach has been working great in my app for several months now.

hey liamj, bottoom sheet behavior might be worth a shot. it’s in the material lib and can be added to your base act. states gotta be handled well as user switches screens. u might need to add a bit extra logic for proper state sync. luck!

I’ve dealt with this exact issue in a music streaming app I developed. My solution was to use a custom ViewGroup that extends FrameLayout, which I called MediaControlBar. I made it a singleton and added it programmatically to each Activity’s root view.

The key was using a bound Service to handle media playback and communicate with the MediaControlBar. I used LiveData to push updates from the Service to the UI, ensuring the control bar stayed in sync across activities.

One gotcha: make sure to handle configuration changes properly. I used ViewModel to retain playback state during rotations.

For a slick UX, I implemented a slide-up animation when the bar appears and added swipe-to-dismiss functionality. Users loved these little touches.

It took some refactoring, but the end result was worth it. The persistent control bar significantly improved user engagement with the app’s media features.