Sanat Pandey
Sanat Pandey

Reputation: 2609

Seek Bar is not synchronized with Media Player in android

I am working on Audio Streaming app in which we are using seekbar for showing movements, but when we forcefully change the position of seek bar then it is not able to synchronize with the song playing on media player.

Code:

seekBarObj
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        /*
                         * mHandler.removeCallbacks(mUpdateTimeTask); String[]
                         * duration =
                         * lstSongDetails.get(Integer.parseInt(currentPosition
                         * )).getDuration().split(":"); int intDuration =
                         * Integer
                         * .parseInt(duration[0])*60*1000+Integer.parseInt
                         * (duration[1]);
                         * 
                         * int currentPosition =
                         * utils.progressToTimer(seekBar.getProgress(),
                         * mediaPlayerObj.getDuration());
                         * 
                         * // forward or backward to certain seconds
                         * mediaPlayerObj.seekTo(currentPosition);
                         */
                        // update timer progress again
                        // updateProgressBar();
                        // mHandler.removeCallbacks(mUpdateTimeTask);
                        //seekBarObj.setMax(mediaPlayerObj.getDuration());
                        mHandler.removeCallbacks(mUpdateTimeTask);
                        //seekBarObj.setProgress(seekBar.getProgress());

                        long position = (long)mediaPlayerObj.getDuration() * (seekBarObj.getProgress()) / 1000;
                        mediaPlayerObj.seekTo((int)position);
                    }

                    public void onStartTrackingTouch(SeekBar seekBar) {

                        //mHandler.removeCallbacks(mUpdateTimeTask);
                        //mediaPlayerObj.seekTo(seekBar.getProgress());
                        /*long position = (long)mediaPlayerObj.getDuration() * (seekBarObj.getProgress()) / 1000;
                        mediaPlayerObj.seekTo((int)position);*/
                    }

                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                        /*
                         * mHandler.removeCallbacks(mUpdateTimeTask);
                         * seekBarObj.setProgress(seekBar.getProgress());
                         * mediaPlayerObj.seekTo(seekBar.getProgress());
                         * updateProgressBar();
                         */

                    }
                });

private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            long totalDuration = mediaPlayerObj.getDuration();
            currentDuration = mediaPlayerObj.getCurrentPosition();
            tvSongDurationLeft.setText(""
                    + utils.milliSecondsToTimer(currentDuration));
            int progress = (int) (utils.getProgressPercentage(currentDuration,
                    totalDuration));
            seekBarObj.setProgress((int) (long) currentDuration);
            mHandler.postDelayed(this, 100);
        }
    };

Upvotes: 0

Views: 2077

Answers (1)

user1881979
user1881979

Reputation:

Please see this link Media Player Example with Seek Bar. It will show you the formula to calculate how you should increase Seek Bar Progress according to song length.

Upvotes: 1

Related Questions