3

I want to write a program which can pause and play every media players in android. How can i set priority of this program higher than the others? also i wrote the following code

public static final String SERVICECMD = "com.android.music.musicservicecommand";
    public static final String CMDNAME = "command";
    public static final String CMDPAUSE = "pause";
    public static final String CMDPLAY = "play";
    public static final String CMDTOGGLEPAUSE = "togglepause";

  AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (mAudioManager.isMusicActive()) {

            Intent i = new Intent(SERVICECMD);
            i.putExtra(CMDNAME, CMDPAUSE);
            MainActivity.this.sendBroadcast(i);
            Toast.makeText(this, "media player is pause!", Toast.LENGTH_LONG).show();

        }
        if (!mAudioManager.isMusicActive()) {
            Intent i = new Intent(SERVICECMD);


            i.putExtra(CMDNAME, CMDPLAY);
            MainActivity.this.sendBroadcast(i);

            Toast.makeText(this, "media player is play!", Toast.LENGTH_LONG).show();
        }

and it can pause every players but i dont know how to play them again. I can play some players but not all of them i dont know why ???

  • You should read on audio focus https://developer.android.com/training/managing-audio/audio-focus.html#RequestFocus – baldguy Dec 02 '16 at 17:31

1 Answers1

1

Have an onClick for a play and pause button

You'll have to find a way to retrieve all of the MediaPlayer Objects.

Pausing:

mMediaPlayer.pause(); // Pause the MediaPlayer

Playing:

mMediaPlayer.prepare(); // Prepares the MediaPlayer to play a song

** Please have an onPrepare method as well

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
              mediaPlayer.start(); // play!
}

Also, here's a reference for you: https://github.com/naman14/Timber

This open source music app should have most of the relevant resources you need to creating a music player controller or etc. You'll be able to understand how it works.

Also, the code you've presented won't allow me to help you with identifying any issues unless you have not implemented the MediaPlayer Object.

Here's why: Difference between Audiomanager and MediaPlayer

-- EDIT --

Here's how you can stop other MediaPlayer instances via the MediaPlayerRegistry.

Android: How to stop other active media players?

Community
  • 1
  • 1
Nicholas
  • 1,883
  • 21
  • 39
  • thank u for your help. but i do not understand correctly the code u sent can that code pause and play the programs out of my application? i mean, whenever i (for example) push the puase and play button in my application, i want all the other media players out of my application (which are running at that time) to be pused or played .... does your code do that? or is this only for inside of my application? – shahab mohseni Dec 02 '16 at 20:38
  • @shahabmohseni nope it doesn't. Check out my answer again, I added the solution to stopping other mediaplayers. – Nicholas Dec 03 '16 at 02:43
  • I want resume other media pelyer – shahab mohseni Dec 06 '16 at 21:57