Search code examples
androidandroid-mediaplayerseekbar

SeekBar is not updating music is playing but the seekBar stay idle


I am playing mp3 file from url, but SeekBar is not updating while playing song.

It showing a buffering but not moving automatically when song starts.

When i am trying to move forcefully then also it working fine.

Below code i am using to play and update SeekBar.

I wanted to create a seekBar that track the progress of a mediaplayer but it doesnt work out quite well, the music is playing but the seekbar stay idle. Is there something that I left out?

Please help me i am new in android.

public class XYZ extends Fragment implements MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnCompletionListener{
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int mediaFileLength;
final Handler handler = new Handler();
private int realtimeLength;
Button b,b3,b4;
private double startTime = 0;
private double finalTime = 0;
public static int oneTimeOnly = 0;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_outside, container,
            false);
    rootView.setBackgroundResource(R.drawable.jotirling);
    seekBar = rootView.findViewById(R.id.seekbar);
    seekBar.setMax(99); // 100% (0~99)
    seekBar.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mediaPlayer.isPlaying()) {
                SeekBar seekBar = (SeekBar) v;
                int playPosition = (mediaFileLength / 100) * seekBar.getProgress();
                mediaPlayer.seekTo(playPosition);
            }
            return false;
        }
    });


    b3.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            final ProgressDialog mDialog = new ProgressDialog(getActivity());


            @SuppressLint("StaticFieldLeak") AsyncTask<String, String, String> mp3Play = new AsyncTask<String, String, String>() {

                @Override
                protected void onPreExecute() {
                    mDialog.setMessage("Please wait It will take time according to your network speed...!");
                    mDialog.show();
                    mDialog.setCancelable(false);
                }

                @Override
                protected String doInBackground(String... params) {
                    try {
                        mediaPlayer.setDataSource(params[0]);
                        mediaPlayer.prepare();
                    } catch (Exception ignored) {

                    }
                    return "";
                }

                @Override
                protected void onPostExecute(String s) {
                    mediaFileLength = mediaPlayer.getDuration();
                    realtimeLength = mediaFileLength;
                    if (!mediaPlayer.isPlaying()) {
                        p=1;
                        mediaPlayer.start();
                        Toast.makeText(getActivity(), "Playing sound", Toast.LENGTH_SHORT).show();
                        finalTime = mediaPlayer.getDuration();
                        startTime = mediaPlayer.getCurrentPosition();
                        b3.setBackgroundResource(R.drawable.pp);
                        if (oneTimeOnly == 0) {
                            oneTimeOnly = 1;
                        }
                    } else {
                        p=0;
                        mediaPlayer.pause();
                        Toast.makeText(getActivity(), "Pausing "+
                                "sound",Toast.LENGTH_SHORT).show();
                        b3.setBackgroundResource(R.drawable.p);
                    }

                    updateSeekBar();
                    mDialog.dismiss();
                }
            };

            mp3Play.execute("URL"); // direct link mp3 file

        }
    });

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);



    return rootView;
}

private void updateSeekBar() {
    seekBar.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / mediaFileLength) * 100));
    if (mediaPlayer.isPlaying()) {
        Runnable updater = new Runnable() {
            @Override
            public void run() {
                updateSeekBar();


            }

        };
    }
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    seekBar.setSecondaryProgress(percent);
}

}

Solution

  • You are defining Runnable updater but not calling it.

    if (mediaPlayer.isPlaying()) {
        Runnable updater = new Runnable() {
            @Override
            public void run() {
                updateSeekBar();
    
    
            }
    
        };
        seekBar.post(updater)
    }