1

I am displaying a VideoView in a Dialog and i am attaching a media control to it.

But when i try to tap on the media controls(play, seekbar etc) the dialog gets dismissed.

The media control buttons don't get tapped, instead the tap registers as an OutsideTouch to the Dialog.

Can anyone help me with this?

What i actually want to achieve are the following:

1) Display a video in a popup with a blurred background. 2) Detect any tap outside the VideoView and popup a "Cancel??" message to user.

Kiran
  • 191
  • 2
  • 10

2 Answers2

3

the dialog layout look 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 Dilalog Frgament make a instance of videoview,

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

next set video uri from local or play using video url, after that,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();
                }
            });
Ajith K P
  • 398
  • 3
  • 12
1

I found a different way to achieve what i wanted.

I placed the videoview in a separate acivity and set the theme for the activity in manifest as

android:theme="@style/Theme.Transparent"

and added the following in res/values/styles.xml

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/transparent1</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
  </style>

Next i overrided the onTouchEvent function as follows:

@Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            int[] l = new int[2];
            v.getLocationInWindow(l);
            int x = l[0];
            int y = l[1];
            int w = v.getWidth();
            int h = v.getHeight();

            if ((int) event.getX() > x && (int) event.getX() < (x + w)
                    && (int) event.getY() > y && (int) event.getY() < (y + h)) {
                // touch is inside the videoview

            } else {
                // touch is outside the videoview
            }

            break;
        case MotionEvent.ACTION_MOVE:

            break;
        case MotionEvent.ACTION_UP:

            break;
        }

        return false;
    }

where "v" is the videoview.

Kiran
  • 191
  • 2
  • 10