ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决Change style of android MediaController

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android MediaController

android 的mediaPlayer 有自带的MediaControl.我们不比自己重写这个控件: A view containing controls for a MediaPlayer. Typically contains the buttons lik

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

android android-widget
|
  this question
edited Dec 29 '13 at 13:20 Benjamin 12.3k 16 97 184 asked Jan 11 '10 at 18:37 duessi 3,121 1 12 8      Here is an example MediaController customization: stackoverflow.com/questions/12482203/… –  Blago Jan 14 '13 at 17:15

 | 

3 Answers
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.


|
  this answer
edited Jan 11 '10 at 23:31 answered Jan 11 '10 at 20:00 James 811 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

 | 

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);

|
  this answer
edited Jan 3 '13 at 10:58 answered Oct 3 '12 at 23:28 bk138 1,826 18 19      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 3 '13 at 10:59  |  show more comments

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.


|
  this answer
answered Mar 23 '16 at 8:37 OneWorld 10.3k 14 57 108

 | 

推荐:android widget

  在Android 1.5 SDK preview中,我们看到了一系列功能和API上的变化变化,包括软键盘、桌面Widget和Live Folder API、视频录制API,蓝牙功能升级等,Google近


相关阅读排行


相关内容推荐