Java – How do I control Android MediaPlayer?

How do I control Android MediaPlayer?… here is a solution to the problem.

How do I control Android MediaPlayer?

I just started developing with Java and Eclipse for Android.

I’m trying to develop an app that controls the built-in Android media player. When I press volume up/down, it should skip the track. How do I control a media player by code?

Solution

Use the Intent of the Media button:

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);

i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,     KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);
 }

Related Problems and Solutions