Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - MediaController of VideoView within dialog appears behind the dialog

I have a VideoView inside a custom dialog, and I am creating a media controller for the VideoView on the fly and assigning it to the VideoView in the code, however the controller doesn't actually appear over the video - it appears behind the dialog! Any idea how to get the controller above the video?

I created a static dialog helper class to help construct the custom dialogs:

public class DialogHelper {

    public static Dialog getVideoDialog(Context context, Uri videoLocation, boolean autoplay) {
        final Dialog dialog = getBaseDialog(context,true, R.layout.dialog_video);

        ((Activity)context).getWindow().setFormat(PixelFormat.TRANSLUCENT);
        final VideoView videoHolder = (VideoView) dialog.findViewById(R.id.video_view);
        videoHolder.setVideoURI(videoLocation);
        //videoHolder.setRotation(90);
        MediaController mediaController =  new MediaController(context);
        videoHolder.setMediaController(mediaController);
        mediaController.setAnchorView(videoHolder);
        videoHolder.requestFocus();
        if(autoplay) {
            videoHolder.start();
        }
        videoHolder.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                dialog.dismiss();

            }
        });
        return dialog;
    }


    /**
     * Creates a simple dialog box with as many buttons as you want
     * @param context The context of the dialog
     * @param cancelable whether the dialog can be closed/cancelled by the user
     * @param layoutResID the resource id of the layout you want within the dialog
     * 
     * @return the dialog
     */
    public static Dialog getBaseDialog(Context context, boolean cancelable, int layoutResID) {
        Dialog dialog = new Dialog(context, R.style.Theme_PopUpDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setContentView(layoutResID);

        return dialog;
    }
}

So in my Activity i just have this to create my dialog:

Dialog videoDialog = DialogHelper.getVideoDialog(context, Uri.parse("http://commonsware.com/misc/test2.3gp"), true);
videoDialog.show();
like image 632
AndroidNoob Avatar asked May 17 '12 10:05

AndroidNoob


1 Answers

This is worked for me

in xml layout make changes like this,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="640dp"
        android:layout_height="400dp"
        android:layout_centerInParent="true" >
    </VideoView>

        <FrameLayout
            android:id="@+id/videoViewWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
        </FrameLayout>

    </RelativeLayout>

in your dilog ,

mVideoView = (VideoView) view.findViewById(R.id.videoview);

in this,video view use setOnPreparedListener listener and set media controller,

mVideoView.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                        @Override
                        public void onVideoSizeChanged(MediaPlayer mp,
                                int width, int height) {
                            /*
                             * add media controller
                             */
                            mc = new MediaController(MainActivity.this);
                            mVideoView.setMediaController(mc);
                            /*
                             * and set its position on screen
                             */
                            mc.setAnchorView(mVideoView);

                            ((ViewGroup) mc.getParent()).removeView(mc);

                            ((FrameLayout) findViewById(R.id.videoViewWrapper))
                                    .addView(mc);
                            mc.setVisibility(View.VISIBLE);
                        }
                    });
                    mVideoView.start();
                }
            });
like image 51
Ajith K P Avatar answered Sep 28 '22 19:09

Ajith K P