Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display current time of song in TextView?

How to display current time of song in TextView with forme "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"
}
like image 709
Mester Hassan Avatar asked Jan 11 '23 08:01

Mester Hassan


1 Answers

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

All the methods implemented into above handler:

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

and another is

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

Hope this helps

like image 62
M D Avatar answered Jan 22 '23 17:01

M D