开发者

Android MediaPlayer custom control panel hide

开发者 https://www.devze.com 2023-04-08 00:48 出处:网络
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 a

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开发者_如何学Python() {
                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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号