Natarajan Raman Natarajan Raman - 1 year ago 251
Android Question

Android MediaPlayer SeekTo function workaround

I am creating a very simple media player app. I would like to resume the song from the SeekTo position that I have captured using getCurrentPosition. However seekTo does not start from the position I have captured but from the beginning.

Code to capture current position, pause and change button text to Click to resume

int media_length = mediaplayer.getCurrentPosition();
Toast.makeText(this,"media length is"+media_length, Toast.LENGTH_LONG).show();
mbutton.setText("Click to Resume");

Code to seekTo captured position, start and change button text to Click to Pause

mbutton.setText("Click to Pause");

A couple of posts are there already related to it but they seem to claim a bug in Android.
Ref: MediaPlayer seekTo doesn't work and
is there any workaround for this? appreciate any help.

Answer Source

You can try below code. It is working for me...

public void forwardSong() {
    if (mPlayer != null) {
        int currentPosition = mPlayer.getCurrentPosition();
        if (currentPosition + seekForwardTime <= mPlayer.getDuration()) {
            mPlayer.seekTo(currentPosition + seekForwardTime);
        } else {

You can pause mediaplayer before this and just call start method after this method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download