10

Is there a way to customize the MediaController? I need to change the style of buttons, SeekBar etc.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
duessi
  • 6,050
  • 3
  • 18
  • 9
  • Here is an example MediaController customization: http://stackoverflow.com/questions/12482203/how-to-create-custom-ui-for-android-mediacontroller/14323144#14323144 – Blago Jan 14 '13 at 17:15

3 Answers3

8

What you can do is recurse the view hierarchy of your MediaController and set the SeekBar's drawable programmatically:

private void styleMediaController(View view) {
    if (view instanceof MediaController) {
        MediaController v = (MediaController) view;
        for(int i = 0; i < v.getChildCount(); i++) {
            styleMediaController(v.getChildAt(i));
        }
    } else
        if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for(int i = 0; i < ll.getChildCount(); i++) {
                styleMediaController(ll.getChildAt(i));
            }
        } else if (view instanceof SeekBar) {
            ((SeekBar) view).setProgressDrawable(getResources().getDrawable(R.drawable.progressbar)); 
            ((SeekBar) view).setThumb(getResources().getDrawable(R.drawable.progresshandle));
        }
}

Then, just call

styleMediaController(myMC);
bk138
  • 3,033
  • 1
  • 34
  • 28
  • Your code does not work for me. What is the reason for the paremeter `index`? `v.getChildCount()` returns 0 everytime. – rekire Dec 28 '12 at 12:02
  • @rekire The index parameter is needed because the function gets called recursively by itself, descending the view hierarchy of the MediaController view. – bk138 Dec 29 '12 at 14:21
  • 1
    But the variable `index` is never used. – rekire Dec 29 '12 at 14:39
  • @rekire It is, see line 5 and 11 of the above snippet ;-) – bk138 Dec 30 '12 at 16:22
  • 1
    Which has some point to it ;-) Edited the post to remove the superfluous argument, thanks! – bk138 Jan 03 '13 at 10:59
  • 2
    Had to call `styleMediaController(myMC)` in the `OnPreparedListener` of the `VideoView` to make it work. Otherwise the MediaController view has no children. – OneWorld Mar 23 '16 at 07:57
  • That really is a terrible solution in terms of almost everything (readability, stability etc.). Yet it seems that framework has left us with no choice. Thanks for sharing – Kirill Starostin Apr 11 '19 at 11:16
3

The method makeControllerView was meant to be overridden so you could supply your own view. Unfortunately, it is hidden at the moment.

You may want to take the source of MediaController and either reimplement it or copy-and-paste the hidden methods into a subclass so you can customize it.

James
  • 841
  • 4
  • 5
  • `makeControllerView` is `protected` for the source I'm looking at. Does anybody know when it changed from `private` to `protected`? – William Jan 29 '15 at 20:22
  • Oh, perhaps you are talking about the @hide in the javadoc for the `makeControllerView` in `android.widget.MediaController`. While this javadoc bit appears to stop the `@Override` annotation from working, it does *not* actually prevent overriding the method. Original question stands almost unchanged - was this ever private? – William Jan 29 '15 at 22:41
3

I changed the code of bk138's answer to just change the color of the elements. Not the drawables themselves. This solution is compatible to old devices in conjunction with the support library v4.

private void styleMediaController(View view) {
        if (view instanceof MediaController) {
            MediaController v = (MediaController) view;
            for (int i = 0; i < v.getChildCount(); i++) {
                styleMediaController(v.getChildAt(i));
            }
        } else if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for (int i = 0; i < ll.getChildCount(); i++) {
                styleMediaController(ll.getChildAt(i));
            }
        } else if (view instanceof SeekBar) {
            ((SeekBar) view)
                    .getProgressDrawable()
                    .mutate()
                    .setColorFilter(
                            getResources().getColor(
                                    R.color.MediaPlayerMeterColor),
                            PorterDuff.Mode.SRC_IN);
            Drawable thumb = ((SeekBar) view).getThumb().mutate();
            if (thumb instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
                //compat mode, requires support library v4
                ((android.support.v4.graphics.drawable.DrawableWrapper) thumb).setCompatTint(getResources()
                        .getColor(R.color.MediaPlayerThumbColor));
            } else {
                //lollipop devices
                thumb.setColorFilter(
                        getResources().getColor(R.color.MediaPlayerThumbColor),
                        PorterDuff.Mode.SRC_IN);
            }
        }
    }

Then, just call

styleMediaController(myMC);

Had to call styleMediaController(myMC) in the OnPreparedListener of the VideoView to make it work. Otherwise the MediaController view has no children.

OneWorld
  • 17,512
  • 21
  • 86
  • 136