Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change style of android MediaController

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

like image 986
duessi Avatar asked Jan 11 '10 18:01

duessi


3 Answers

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);
like image 55
bk138 Avatar answered Nov 09 '22 19:11

bk138


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.

like image 29
James Avatar answered Nov 09 '22 18:11

James


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.

like image 3
OneWorld Avatar answered Nov 09 '22 17:11

OneWorld