ufukcakir ufukcakir - 6 months ago 24
Android Question

forward, backward buttons in android music player project

I have a music player application that coded in Eclipse. App has buttons such as Start/Pause, Next song, Previous song, Mixer. I want to add forward and backward buttons, how may i do it with the easiest way?

Thanks all.

Answer

To fast forward the song:

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

To rewind the song:

public void rewindSong() {
    if (mPlayer != null) {
        int currentPosition = mPlayer.getCurrentPosition();
        if (currentPosition - seekBackwardTime >= 0) {
            mPlayer.seekTo(currentPosition - seekBackwardTime);
        } else {
            mPlayer.seekTo(0);
        }
    }
}

mPlayer is the object of MediaPlayer.

seekForwardTime and seekBackwardTime are two variable to forward or rewind the song for defined second..

private int seekForwardTime = 5 * 1000; // default 5 second
private int seekBackwardTime = 5 * 1000; // default 5 second

Hope this will help...