Android MediaPlayer自定义控制面板隐藏

发布于 2024-12-07 02:56:25 字数 681 浏览 2 评论 0原文

我为视频播放器创建了一个自定义控制面板。现在我想提供像默认 MediaController 一样的效果,其中当触摸屏幕时面板变得可见,并且在最后一次触摸时间后它再次变得不可见。我可以使用这种类型的代码。

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

我可以在触摸屏幕时启动线程,并在 60 秒后使其不可见。但就我而言,如果用户在这 60 秒内再次触摸屏幕,面板应在上次触摸后 60 秒后消失。这种情况又该如何考虑呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷→糖丶微凉 2024-12-14 02:56:25

我建议结合使用 Runnable 和 Handler。您可以使用 postDelayed() 进行 Handler 调用,以便在 60 秒后执行某些操作。

这是一个例子:

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

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
   }
};
少女的英雄梦 2024-12-14 02:56:25

只需删除/取消当前计时器即可。

顺便说一句,您不应该通过线程来完成此操作,而应该通过将消息发布到处理程序来完成。这样的未来定时器任务不需要另一个线程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文