【问题标题】:show time when song is playing in android显示在android中播放歌曲的时间
【发布时间】:2011-05-19 14:51:47
【问题描述】:

我正在尝试使用给定列表中的 MediaPlayer 播放歌曲。我希望包括歌曲从开始到结束的持续时间。如何添加时间以及如何更新从 0:00 到该歌曲结束的时间

【问题讨论】:

标签: android media-player


【解决方案1】:

您可以使用getCurrentPosition() 方法,它以毫秒为单位为您提供当前位置。

您也可以使用getDuration()的方法获取歌曲的全长。

您可以使用单独的线程来更新计时器。

【讨论】:

    【解决方案2】:

    为了显示播放时间,我使用了 Timer 和 TimerTask,它们每 1000 毫秒更新一次 TextView tv。请注意,不使用tv.post(Runnable action) 在 Text View 中设置文本不会阻塞 UI 线程并且可能会导致问题。

    if (player != null) {
        player.start();
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (player != null && player.isPlaying()) {
                            tv.post(new Runnable() {
                                @Override
                                public void run() {
                                    tv.setText(player.getCurrentPosition());
                                }
                            });
                        } else {
                            timer.cancel();
                            timer.purge();
                        }
                    }
                });
            }
        }, 0, 1000);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      相关资源
      最近更新 更多