Search code examples
androidandroid-videoviewseekbarmediacontroller

How to show an Alert message to user when clicks or touches the seekbar of default Mediacontroller with videoview?


I am implemented VideoView with Mediacontroller like below.

MediaController mediacontroller = new MediaController(VideoViewActivity.this);
mediacontroller.setAnchorView(mVideoView);
mVideoView.setMediaController(mediacontroller);

Now I want to show an Alert When user clicks on seekbar. I have tried with following code by adding onclickListener to seekbar, but not working.

int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
SeekBar seekbar = (SeekBar) mediacontroller.findViewById(topContainerId);
seekbar.setOnClickListener(new View.OnClickListener() {
     @Override
       public void onClick(View v) {
            Log.i(TAG, "WE HAVE LIFT OFF");
            showAlertMesage();
           }
     });

But the event is not firing hence alert message with 'Ok' and 'Cancel' options not showing.

Also implementation should be showing an alert when user clicks/touches the seekbar.The seekbar need not to be seekable till user clicks 'OK'.

If user clicks 'Cancel' Video playing and seeking position should not be change. if clicks on 'Ok' it should be seekable to that postion.

So kindly please share or suggest How to implement this behaviour, Thank you all in advance.


Solution

  • Your approach is correct. But you need to do it after your video is prepared.Make use of OnPreparedListener

    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mVideoView.start();
                    int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
                    SeekBar seekbar = (SeekBar) mediacontroller.findViewById(topContainerId);
                     seekbar.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                           Log.i(TAG, "WE HAVE LIFT OFF");
                             if(seekAllowed){ //declare seekAllowed as false globally
                                 return false;
                              }
                            webView1.pause();
                            showAlertMesage();
                            return true;
                        }
                    });
                }
            });
    

    Dialog:

    private void showAlertMesage() {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            seekAllowed = true;
                            webView1.resume();
                        }
                    });
    
            alertDialogBuilder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
    
                            webView1.resume();
                        }
                    });
    
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }