1

I would like to display the duration of the song.

public class Songs extends AppCompatActivity {
    MediaPlayer song;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surah);
        song=MediaPlayer.create(Songs.this,R.raw.song);
    }
    public void play1(View v){
        song.start();
    }
    public void pause1(View v){
        song.pause();
    }
}
frogatto
  • 28,539
  • 11
  • 83
  • 129

1 Answers1

0

You can use Timer which will be updated every 1000 milliseconds. Then set time to a TextView or what ever you want.

song.start();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (song != null && song.isPlaying()) {
                    tv.post(new Runnable() {
                        @Override
                        public void run() {
                            tv.setText(song.getCurrentPosition());
                        }
                    });
                } else {
                    timer.cancel();
                    timer.purge();
                }
            }
        });
    }
}, 0, 1000);
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53