SeekBar没有更新Android

时间:2015-03-19 09:09:05

标签: android seekbar

我的SeekBar项目中有ListView。每首项目都有一首歌。播放歌曲时,我希望SeekBar使用MediaPlayer更新和/或控制SeekBar进度。 到目前为止,我只是通过拖动拇指设置MediaPlayer进度来控制它。问题是,我无法让它更新SeekBar Thumb位置。

这是我的代码:

    Ids holder;
    private final Utils utils = new Utils();
    private final Handler handler = new Handler();
    long totalDuration;
    long currentPosition;

...

holder.seekbar.setMax(100);
        holder.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                handler.removeCallbacks(updateTimeProgressBar);
                int fullDuration = Item.mp.getDuration();
                int currentPosition = utils.progressToTimer(
                        seekBar.getProgress(), fullDuration);
                Item.mp.seekTo(currentPosition);
                updateProgressBar();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                handler.removeCallbacks(updateTimeProgressBar);

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {

            }
        });

        holder.seekbar.setFocusable(true);
        holder.seekbar.setEnabled(true);
        holder.seekbar.setTag(position);

... 播放文件

Item.mp = MediaPlayer.create(context,
                        Integer.valueOf(this.songPos[songPosInt]).intValue());
                Item.mp.start();
                holder.seekbar.setProgress(0);
                updateProgressBar();

和finaly代码更新SeekBar

public void updateProgressBar() {
        handler.postDelayed(updateTimeProgressBar, 1000);

    }

    private final Runnable updateTimeProgressBar = new Runnable() {
        @Override
        public void run() {
            if (Item.isPlaying == true && pausedSamePos == false) {
                currentPosition = Item.mp.getCurrentPosition();
                totalDuration = mp.getDuration();
                int currentTime = (utils.getProgressPercentage(totalDuration,
                        currentPosition));
                holder.seekbar.setProgress(currentTime);
                Log.i("CALLED", "SHOULD BE UPDATED " + currentPosition
                        + "of total" + totalDuration);
                handler.postDelayed(this, 1000);

            }
        }
    };

Utils是一个简单的助手类,可以将MS转换为SEC 我也试过

int totalDuration=mp.getDuration() /1000;
holder.seekbar.setMax(totalduration);
..
int currentPosition=mp.getCurrentPosition()/1000;
holder.seekbar.setProgress(currentPosition);

搜索条

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_frame3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:paddingLeft="5.0dip"
        android:paddingRight="5.0dip"
        android:progressDrawable="@drawable/seekbar_line"
        android:thumb="@drawable/seekbar_thumb" />

    <TextView
        android:id="@+id/textview_duration_sb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10.0dip"
        android:text="00:05"
        android:textAppearance="?android:textAppearanceSmall"
        android:textColor="#8C000000" />

</LinearLayout>

在我的ListView视图

 <FrameLayout
                android:id="@+id/main_frame2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5.0dip" >

                <TextView
                    android:id="@+id/textview_duration"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:focusable="false"
                    android:text="00:05"
                    android:textColor="#8C000000"
                    android:textSize="14.0sp" />

               <include layout="@layout/play_item" />
            </FrameLayout>

3 个答案:

答案 0 :(得分:3)

问题是,错误SeekBar正在更新。因此,只需在SeekBar内添加runnable监听器和clickListener即可。还要扩展基本适配器并获取项目ID。

答案 1 :(得分:2)

这是我的实施,它为我工作:

在代码中的某处添加此内容(每首歌一次):

_seekBar.setProgress(0);
    _seekBar.setMax(_mediaPlayer.getDuration() / 1000);

然后使用以下代码更新搜索栏:

private Runnable _updateRunnable = new Runnable() {
    public void run() {
        if (_hasStopped) {
            return;
        }

        updateSeekbarView();
    }
};

private synchronized void updateSeekbarView() {
    if (_hasStopped) {
        reset();
        return;
    }

    if (_mediaPlayer == null) {
        return;
    }

    if (!_mediaPlayer.isPlaying()) {
        stop();
        return;
    }

    long totalDuration = _mediaPlayer.getDuration();
    long currentDuration = _mediaPlayer.getCurrentPosition();

    _txtMax.setText("" + AudioUtils.milliSecondsToTimer(totalDuration));
    _txtCurrent.setText("" + AudioUtils.milliSecondsToTimer(currentDuration));
    _seekBar.setProgress((int) (currentDuration / 1000));
    _handler.postDelayed(_updateRunnable, 1000);
}

以及AudioUtils.milliSecondsToTimer()实施,以防您希望将计时器显示为搜索栏旁边的文字:

public static String milliSecondsToTimer(long milliseconds) {
    String finalTimerString = "";
    String secondsString = "";

    // Convert total duration into time
    int hours = (int) (milliseconds / (1000 * 60 * 60));
    int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
    int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
    // Add hours if there
    if (hours > 0) {
        finalTimerString = hours + ":";
    }

    // Prepending 0 to seconds if it is one digit
    if (seconds < 10) {
        secondsString = "0" + seconds;
    } else {
        secondsString = "" + seconds;
    }

    finalTimerString = finalTimerString + minutes + ":" + secondsString;

    // return timer string
    return finalTimerString;
}

答案 2 :(得分:1)

在这种情况下,我认为持有者引用被转储或更改为另一个视图。

您可以通过在getView方法中初始化Runnable来解决此问题,如下所示:

View getView(){
...
updateTimeProgressBar = new Runnable() {
        Ids holderCopy = holder;
        @Override
        public void run() {
            if (Item.isPlaying == true && pausedSamePos == false) {
                currentPosition = Item.mp.getCurrentPosition();
                totalDuration = mp.getDuration();
                int currentTime = (utils.getProgressPercentage(totalDuration,
                        currentPosition));
                holderCopy.seekbar.setProgress(currentTime);
                Log.i("CALLED", "SHOULD BE UPDATED " + currentPosition
                        + "of total" + totalDuration);
                handler.postDelayed(this, 1000);

            }
        }
    };
}

但我真的需要知道你是如何演奏你的歌曲才能进一步帮助你...