简体   繁体   中英

Mediaplayer seekbar seek not working properly

currently I'm trying to fix a bug at my seekbar, when I click at determinated position, the seekBar & mediaPlayer reset to 0. Check my code below:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        final Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                if (mediaPlayer != null) {
                    int CurrentPosition = mediaPlayer.getCurrentPosition();
                    int Duration = mediaPlayer.getDuration();

                    int progress = (getProgressPercentage(CurrentPosition, Duration));
                    seekBar.setProgress(progress); // this line is working perfectly, I'm getting a high accuracy using getProgressPercentage

                    String m_1;
                    String s_1;
                    //Toast.makeText(getApplicationContext(), "Current: " + CurrentPosition + " - Duration: " + mediaPlayer.getDuration(), Toast.LENGTH_LONG).show();

                    final int minutes_1 = (CurrentPosition / 1000) / 60;
                    final int seconds_1 = ((CurrentPosition / 1000) % 60);

                    if (minutes_1 < 10) {
                        m_1 = "0" + minutes_1;
                    } else {
                        m_1 = "" + minutes_1;
                    }

                    if (seconds_1 < 10) {
                        s_1 = "0" + seconds_1;
                    } else {
                        s_1 = "" + seconds_1;
                    }
                    time1.setText(m_1 + ":" + s_1);


                    String m_2;
                    String s_2;
                    final int minutes_2 = (Duration / 1000) / 60;
                    final int seconds_2 = ((Duration / 1000) % 60);

                    if (minutes_2 < 10) {
                        m_2 = "0" + minutes_2;
                    } else {
                        m_2 = "" + minutes_2;
                    }

                    if (seconds_2 < 10) {
                        s_2 = "0" + seconds_2;
                    } else {
                        s_2 = "" + seconds_2;
                    }

                    time2.setText(m_2 + ":" + s_2);

                }
                mHandler.postDelayed(this, 1000);
            }
        };
        mRunnable.run();

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mediaPlayer != null && fromUser) {

                    int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration()));
                    mediaPlayer.seekTo(_progress); //the problem is here, the seetkTo is reseting to zero.
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });


    }


    public int getProgressPercentage(long currentDuration, long totalDuration){
        Double percentage = (double) 0;

        long currentSeconds = (int) (currentDuration / 100);
        long totalSeconds = (int) (totalDuration / 100);

        percentage =(((double)currentSeconds)/totalSeconds)*100;

        return percentage.intValue();
    }

There are no errors in this script, everything is working perfeclty, except when I click at determinated position, I'm using int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration())); since getProgressPercentage is working fine to me at final Runnable mRunnable = new Runnable() { . I'm getting more accuracy using this function.

So, the unique problem is related to seekBar.setOnSeekBarChangeListener . I don't know how to fix this, can you help me? Thank you.

int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration()));
mediaPlayer.seekTo(_progress); //the problem is here, the seetkTo is reseting to zero.

_progress in your case is the percentage of media that has been already played, and not the absolute duration in millis . mediaPlayer.seekTo(long millis) expects the time in millis . Instead you are passing the percentage. Change it like this:

 int _progress = (progress * mediaPlayer.getDuration())/100;
    mediaPlayer.seekTo(_progress); // PROBLEM SOLVED

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM