Show the time when a song is playing in android

I am trying to play a song using the MediaPlayer from this list. I want to include the length of time for the song from beginning to end of the song. how to add time and how to update this time from 0:00 to the end of this song

+6
source share
3 answers

You can use the getCurrentPosition() method, which gives you the current position in milliseconds.

You can also use the getDuration() method to get the full length of the song.

You can use a separate thread to update the timer.

+17
source

To show playback time, I used Timer and TimerTask , which updates the TextView tv every 1000 ms. Please note that not using tv.post (Runnable action) to set text in text form will not block the user interface stream and may cause problems.

 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); } 
+6
source

Give this solution. According to the post, it only works for Android 1.5, but this should indicate the right direction. The problem is that there is no documentary way (which I know) about currently playing track information, although I really did not use the audio part of the Android SDK, as my projects require concentration in other places.

-1
source

Source: https://habr.com/ru/post/888547/


All Articles