Seek Bar与android中的Media Player不同步

时间:2012-12-04 11:14:50

标签: android seekbar

我正在使用音频流应用程序,我们使用搜索栏来显示移动,但是当我们强行改变搜索栏的位置时,它就无法与媒体播放器上播放的歌曲同步。

代码:

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

1 个答案:

答案 0 :(得分:1)

请参阅此链接Media Player Example with Seek Bar。它将显示计算如何根据歌曲长度增加搜索栏进度的公式。