shahab mohseni
shahab mohseni

Reputation: 31

how to pause and play music player

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 ???

Upvotes: 3

Views: 3123

Answers (1)

Nicholas
Nicholas

Reputation: 1980

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?

Upvotes: 1

Related Questions