Mediaplayer搜寻栏搜寻无法正常工作

时间:2018-10-01 20:11:21

标签: java android android-mediaplayer

当前,我正在尝试修复我的seekbar上的错误,当我在确定的位置单击时,seekBar&mediaPlayer重置为0。请检查以下代码:

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();
    }

此脚本中没有错误,一切正常,但当我单击确定位置时,我正在使用int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration()));,因为getProgressPercentagefinal Runnable mRunnable = new Runnable() {上可以正常工作。使用此功能,我的准确性更高。

因此,唯一的问题与seekBar.setOnSeekBarChangeListener有关。我不知道该如何解决,您能帮我吗?谢谢。

1 个答案:

答案 0 :(得分:1)

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

_progress是您已经播放的媒体百分比,而不是absolute duration in millismediaPlayer.seekTo(long millis)预计在millis中的时间。相反,您是通过百分比。像这样更改它:

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