开发者

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

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.isP开发者_运维知识库laying()) {
            //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
    }

});


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.


I think you have to make a thread... I have some code given below which you can try to implement.

public void run() {
    try
    {
        while(song1.getDuration()!=song1.getCurrentPosition())
        {
            skbar.setProgress(song1.getCurrentPosition());
            //bStop.setText(song1.getCurrentPosition());
        }
        if (song1.getDuration()==song1.getCurrentPosition())
            Log.v("log","Sanket");
        t.suspend();
    }
    catch (Exception e)
    {
        Log.e("log",e.toString());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜