Search code examples
androidandroid-animationandroid-videoviewmediacontroller

MediaController doesn't respect view's animation


The Question says it all, MediaController doesn't follow the animation applied to the view.

I've a RelativeLayout inside which I've a VideoView. MediaController is anchored to this VideoView. Now, in the activity, I bring the relative layout on the screen with some animation(say right to left). All the things inside the layout, like textview, videoview animates perfectly, but MediaController doesn't. Instead of appearing from right to left alongwith VideoView, it just appears in between the screen while the VideoView is still appearing from right to left.

Is there any way to force the animation to be applied to the MediaController.

Minimal Structure ->

RelativeLayout
|
|--- TextView
|--- VideoView + MediaController
|--- Some other view

I apply the animation on the relative Layout.

rtlAnim = AnimationUtils.loadAnimation(activity, R.anim.right_to_left);
relativeLayout.startAnimation(rtlAnim);

right_to_left.xml :

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="500"/>
</set>

Any help will be appreciated.

PS : I show the mediacontroller once the videoview is prepared.

videoView.setOnPreparedListener(mediaPlayer -> mediaController.show());

Solution

  • Solved it using below code from here. Since mediaController places the controls in a floating window, so we've to manually add it to the view hierarchy. :

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        videoView.setMediaController(ctrl);
                        ctrl.show();
                        FrameLayout frameLayout = (FrameLayout) ctrl.getParent();
                        ((ViewGroup) frameLayout.getParent()).removeView(frameLayout);
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                        params.addRule(ALIGN_BOTTOM, videoView.getId());
                        ((RelativeLayout) videoView.getParent()).addView(frameLayout, params);
                    }
                });