【问题标题】:How to display current time of song in TextView?如何在 TextView 中显示歌曲的当前时间?
【发布时间】:2014-01-30 04:25:25
【问题描述】:

如何在 TextView 中以“hh:mm:ss”形式显示歌曲的当前时间?

Runnable run = new Runnable() {
        @Override
        public void run() {
            seekUpdation();
            compteurCurrentTime = mediaPlayer.getCurrentPosition() / 1000;
            showTimeCurrent();  
        }
    };

    public void seekUpdation() {
        seekbar.setProgress(mediaPlayer.getCurrentPosition());
        seekHandler.postDelayed(run, 1000);   
    }
private void showTimeCurrent() {
//display current time of song in TextView with forme "hh:mm:ss"
}

【问题讨论】:

标签: android media-player


【解决方案1】:

试试这个Handler:

private Runnable mUpdateTimeTask = new Runnable() {
    @Override
    public void run() {
        long totalDuration = MediaAdapter.getMediaPlayer().getDuration();
        long currentDuration = MediaAdapter.getMediaPlayer()
                .getCurrentPosition();

        // Displaying Total Duration time
        songTotalDurationLabel.setText(""
                + utils.milliSecondsToTimer(totalDuration));
        // Displaying time completed playing
        songCurrentDurationLabel.setText(""
                + utils.milliSecondsToTimer(currentDuration));

        // Updating progress bar
        int progress = (utils.getProgressPercentage(currentDuration,
                totalDuration));
        // Log.d("Progress", ""+progress);
        songProgressBar.setProgress(progress);

        // Running this thread after 100 milliseconds
        mHandler.postDelayed(this, 100);
    }
};

上述处理程序中实现的所有方法:

public String milliSecondsToTimer(long milliseconds){
    String finalTimerString = "";
    String secondsString = "";

    // Convert total duration into time
       int hours = (int)( milliseconds / (1000*60*60));
       int minutes = (int)(milliseconds % (1000*60*60)) / (1000*60);
       int seconds = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);
       // Add hours if there
       if(hours > 0){
           finalTimerString = hours + ":";
       }

       // Prepending 0 to seconds if it is one digit
       if(seconds < 10){ 
           secondsString = "0" + seconds;
       }else{
           secondsString = "" + seconds;}

       finalTimerString = finalTimerString + minutes + ":" + secondsString;

    // return timer string
    return finalTimerString;
}

另一个是

public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;

    long currentSeconds = (int) (currentDuration / 1000);
    long totalSeconds = (int) (totalDuration / 1000);

    // calculating percentage
    percentage =(((double)currentSeconds)/totalSeconds)*100;

    // return percentage
    return percentage.intValue();
}

希望对你有帮助

【讨论】:

  • 但这不是一个好的解决方案。它会滞后 UI,最好使用“ScheduledExecutorService”
猜你喜欢
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
相关资源
最近更新 更多