When you start the player, you need to update and display the time of the current composition: in this code, I update seekbar, as I do the same for composition time + implementation millisecondsToTime

public void startMediaPlayer() { mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { seekBar.setMax(mp.getDuration()); seekBar.postDelayed(onEverySecond, 1000); mediaPlayer.start(); } }); } private Runnable onEverySecond = new Runnable() { @Override public void run() { if (seekBar != null) { seekBar.setProgress(mediaPlayer.getCurrentPosition()); } if (mediaPlayer.isPlaying()) { seekBar.postDelayed(onEverySecond, 1000); } } }; 

Markup:

 <TextView android:id="@+id/textPlayer" android:textColor="@color/white" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="00:00" android:textSize="20sp" /> 
  • And what you can not? Judging by the code, the question from the title is not relevant ... - xkor
  • @xkor updated the header and indicated that the code is upward

1 answer 1

Well, that's really the same thing to do)

  if (textView != null) { textView.setText(millisecondsToTime(mediaPlayer.getCurrentPosition())); } 

Or are you just unable to implement millisecondsToTime? Then they would have asked about it. This method will look like this:

 private String millisecondsToTime(int millis) { int min = TimeUnit.MILLISECONDS.toMinutes(millis); int sec = TimeUnit.MILLISECONDS.toSeconds(millis) % 60; return String.format("%02d:%02d", min, sec); } 
  • @upward updated the answer, and I advise you to work on the formulation of your thoughts, I could not understand your last message. - xkor
  • @ xkor textView.setText (millisecondsToTime (mediaPlayer.getCurrentPosition ())); I write this code in the player start method? - upward
  • one
    if you want it to work only when the player starts, then yes, but I think you want to update the time as well as the sikbar) - xkor