0

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

代码:

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

1 回答 1

1

请参阅此链接Media Player Example with Seek Bar。它将向您展示计算应如何根据歌曲长度增加 Seek Bar Progress 的公式。

于 2012-12-06T12:00:12.110 回答