1

This is my VideoplayerActivity:

public class VideoPlayerActivity extends Activity {
    CustomVideoView video_player_view;
    DisplayMetrics dm;
    SurfaceView sur_View;
    MediaController media_Controller;
    AlertDialog alertDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_view);
        if (getIntent().getData() != null && getIntent().getData().getPath() != null) {
            getInit(getIntent().getData().getPath());
        }
        if (getIntent().hasExtra("file_path")) {
            getInit(getIntent().getStringExtra("file_path"));
        }
    }

    public void getInit(String file_path) {
        video_player_view = (CustomVideoView) findViewById(R.id.video_player_view);
        media_Controller = new MediaController(this);
        video_player_view.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

            @Override
            public void onPlay() {
                if (alertDialog != null) {
                    alertDialog.dismiss();
                }
            }

            @Override
            public void onPause() {
                alertDialog = new AlertDialog.Builder(VideoPlayerActivity.this).create();
                alertDialog.setMessage("Video has been paused");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();

                    }
                });
                alertDialog.show();
            }
        });

        video_player_view.setMediaController(media_Controller);
        video_player_view.setVideoPath(file_path);
        video_player_view.start();
    }
}

and here is my CustomVideoView

public class CustomVideoView extends VideoView {

    private PlayPauseListener mListener;

    public CustomVideoView(Context context) {
        super(context);
    }

    public CustomVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setPlayPauseListener(PlayPauseListener listener) {
        mListener = listener;
    }

    @Override
    public void pause() {
        super.pause();
        if (mListener != null) {
            mListener.onPause();
        }
    }

    @Override
    public void resume() {
        super.resume();
        if (mListener != null) {
            mListener.onPlay();
        }
    }

    @Override
    public void start() {
        super.start();
        if (mListener != null) {
            mListener.onPlay();
        }
    }

    public static interface PlayPauseListener {
        void onPlay();
        void onPause();
    }

}

the problem is that when i click to resume video at first it dismiss alert dialog and after second time i clicked it resume my video. I want it to be happen on single click , kindly help me through this.

Jon
  • 9,156
  • 9
  • 56
  • 73

2 Answers2

0

So I'm not sure of a way to show an alert dialog and allow the user to click on ui elements not in the foreground. What you can do is add a dismiss listener that will be called if the user dismisses the dialog on an outside touch and start playback from there.

public void getInit(String file_path) {
    video_player_view = (CustomVideoView) findViewById(R.id.video_player_view);
    media_Controller = new MediaController(this);
    video_player_view.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

        @Override
        public void onPlay() {
            if (alertDialog != null) {
                alertDialog.dismiss();
            }
        }

        @Override
        public void onPause() {
            alertDialog = new AlertDialog.Builder(VideoPlayerActivity.this).create();
            alertDialog.setMessage("Video has been paused");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    video_player_view.start();
                }
            });
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    video_player_view.start();
                }
            });
            alertDialog.show();
        }
    });

    video_player_view.setMediaController(media_Controller);
    video_player_view.setVideoPath(file_path);
    video_player_view.start();
}
Jon
  • 9,156
  • 9
  • 56
  • 73
  • Thanks sir, but i just want it to happen on play button click, is there any way for this – puneet kumar Sep 14 '16 at 14:01
  • @puneetkumar so you want the user to be able to click play on the media controller while there is a dialog in the foreground? – Jon Sep 14 '16 at 14:04
0

This I tested and seems to work! from this post

public void getInit(String file_path) {
    video_player_view = (CustomVideoView) findViewById(R.id.video_player_view);
    media_Controller = new MediaController(this);
    video_player_view.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

        @Override
        public void onPlay() {
            if (alertDialog != null) {
                alertDialog.dismiss();
            }
        }

        @Override
        public void onPause() {
            alertDialog = new AlertDialog.Builder(VideoPlayerActivity.this).create();
            alertDialog.setMessage("Video has been paused");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                }
            });

            alertDialog.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            alertDialog.getWindow()
                    .clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            alertDialog.show();
        }
    });

    video_player_view.setMediaController(media_Controller);
    video_player_view.setVideoPath(file_path);
    video_player_view.start();
}
Community
  • 1
  • 1
Jon
  • 9,156
  • 9
  • 56
  • 73