0

Im trying to get the seekbar to update and show progress whenever I play an mp3 with mediaPlayer. Music plays fine everytime, seekbar will always snap to 0 position when mp3 is playing.

I was trying to follow this answer but it just wont work... SeekBar and media player in android

I have this code in main activity

private Handler mHandler = new Handler();
MainActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (Assets.mediaPlayer != null) {
                int mCurrentPosition = Assets.mediaPlayer.getCurrentPosition() / 1000;
                if(OnionFragment.seekBar!= null) {
                    OnionFragment.seekBar.setProgress(mCurrentPosition);
                }
            }
            mHandler.postDelayed(this, 1000);
        }
    });

in OnionFragment I have

public static SeekBar seekBar;
seekBar = (SeekBar) rootView.findViewById(R.id.seekBar);

and in OnionFragments onclick (the play button)I have

Assets.playMusicFile(MainActivity.items.get(MainActivity.selectedItem).getSongId(), true);
                if(Assets.mediaPlayer!=null) {
                    seekBar.setMax(Assets.mediaPlayer.getDuration());
                }

OnionFragment is loaded into MainActivity right away and looks like this

P.S. If anyone has extra time, how do i change size of seekbar ball and color

Community
  • 1
  • 1
Tintinabulator Zea
  • 2,617
  • 5
  • 18
  • 32

1 Answers1

2

Change

seekBar.setMax(Assets.mediaPlayer.getDuration());

to

seekBar.setMax(Assets.mediaPlayer.getDuration()/1000);
Volker Voecking
  • 5,203
  • 2
  • 38
  • 35