MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.song); mediaPlayer.start(); ... // display current time of song being played int currentPos = mediaPlayer.getCurrentPosition(); // convert milliseconds to minutes and seconds int minutes = (currentPos / 1000) / 60; int seconds = (currentPos / 1000) % 60; String currentTime = String.format("%02d:%02d", minutes, seconds); textView.setText(currentTime);
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.video); mediaPlayer.start(); ... // get current time of video being played int currentPos = mediaPlayer.getCurrentPosition(); ... // seek to desired time in video int desiredTime = 60000; // move video to 1 minute mark mediaPlayer.seekTo(desiredTime);In both examples, the getCurrentPosition() method is used to get the current time of the audio or video being played. This method is part of the android.media package library in Android.