Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use SeekBar's OnSeekBarChangeListener to seek to a specific point in a MediaPlayer object?

Tags:

android

I have some Android code to stream an audio file from the internet and play the stream after 10 seconds.

I am using a SeekBar to view the buffering status and playing status. I want to play the audio starting from the middle of the buffered stream. For that, I move the SeekBar point to the middle, but I cannot play the audio from the middle; it will go back and start from the beginning. How can I get the seeked position and how can I play the audio from that particular position?

Here is my SeekBar code. How can I make this code use the OnSeekBarChangeListener properly?

seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        if (arg2 && mediaPlayer.isPlaying()) {
            //myProgress = oprogress;
            arg1=mediaPlayer.getCurrentPosition();
            mediaPlayer.seekTo(arg1);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

});
like image 752
Raghu Mudem Avatar asked Nov 15 '22 06:11

Raghu Mudem


1 Answers

arg1=mediaPlayer.getCurrentPosition();
mediaPlayer.seekTo(arg1);

You are forcing the player to seek to the current position, and not to the positon retuned by the Seekbar

Remove the line: arg1=mediaPlayer.getCurrentPosition(); and it should work. Of course after MediaPlayer.prepare() set SeekBar.setMax(MediaPlayer.getDuration()), so seeking will be accurate.

like image 137
Greg Avatar answered Jan 03 '23 08:01

Greg