在视频的播放按钮上单击两次以恢复视频

时间:2016-09-14 13:41:01

标签: android

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

这是我的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();
    }

}

问题是,当我点击恢复视频时,它首先关闭警报对话框,第二次点击它后恢复我的视频。 我想让它在单击时发生,请通过这个帮助我。

2 个答案:

答案 0 :(得分:0)

所以我不确定是否有办法显示警告对话框并允许用户点击不在前台的ui元素。你可以做的是添加一个dismiss监听器,如果用户在外部触摸时解除对话并从那里开始播放,将会调用该监听器。

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

答案 1 :(得分:0)

这个我测试过,似乎工作!来自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();
}