Home Android: How to stop other active media players?
Reply: 1

Android: How to stop other active media players?

reiley Published in 2013-02-13 18:17:11Z

I'm trying to create a music player. My application has 2 players -

(a) plays songs from the memory

(b) plays online music streams using URLs

The home/default activity of my app is (a). For player (b) there is a separate activity.

Issue is when user presses the play button in activity (b) to play the URL stream, the track from the memory gets played.

It seems like MediaPlayer from activity (a) is still active.

So I want:

  • If user presses the play button, all other media player gets stopped.

    Is there any way to get reference of other media players; Check if they are active; If yes, then stop them.

From a vague perspective, I thought this can be done by sending a broadcast to the Service of activity (a), and make a function there which will listen it and stop the player.

But it seems a bit brute to me & also how should I get the reference of the player in (a).

Hope I've cleared my doubt.

Please suggest.

Thank You

Mr.Me Reply to 2013-02-13 18:27:24Z

You can create a class in your project and call it MediaPlayerRegistry where you store a reference to each mediaPlayer object you are currently using. and access them anywhere in your app to stop / release / replay them. it would look something like this:

public class MediaPlayerRegistry {
      public static List<MediaPlayer> mList = new ArrayList<MediaPlayer>();

Then from any activity you have you can call:

MediaPlayerRegistry.mList.put(MediaPlayer mPlayer);

to add it to your registry... and when you want to release it stop any running object.

for ( MediaPlayer player : MediaPlayerRegistry.mList) {
if (player != null && player.isplaying() {

just think of it as your own pool of MediaPlayer objects

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315986 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO