dev_android
dev_android

Reputation: 8818

Android MediaPlayer custom control panel hide

I have created a custom control panel for a video player. Now I want to give a effect like default MediaController where the panel becomes visible when the screen is touched and it becomes invisible again after the last touch time. I can use this type of code for that.

Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // make the panel invisible 
                    }
                });
            }
        };

I can start the thread when the screen is touched and make it invisible after 60 seconds. But in my case, if the user touches the screen again in between this 60 seconds, the panel should vanish after 60 seconds from the last touch. How to consider this case also?

Upvotes: 0

Views: 914

Answers (2)

Pointer Null
Pointer Null

Reputation: 40370

Simply delete/cancel current timer.

Btw, you should not do it by Thread, but by posting message to a Handler. Such future timer task doesn't need another thread.

Upvotes: 1

Michell Bak
Michell Bak

Reputation: 13242

I would recommend using a combination of Runnables and a Handler. You can do Handler calls using postDelayed() to do something after, say, 60 seconds.

Here's an example:

private Handler mHandler = new Handler();

mHandler.post(showControls); // Call this to show the controls

private Runnable showControls = new Runnable() {    
   public void run() {
      // Code to show controls
      mHandler.removeCallbacks(showControls);
      mHandler.postDelayed(hideControls, 60000);
   }
};

private Runnable hideControls = new Runnable() {
   public void run() {
      // Code to hide the controls
   }
};

Upvotes: 1

Related Questions