我有一个SeekBar
在我的ListView
项目。并为每个项目的歌曲。当播放歌曲时,我想SeekBar
更新和或控制MediaPlayer
与SeekBar
进步。 到目前为止,我已经使它只能控制,通过拖动拇指来设置MediaPlayer
进度。问题是,我无法让它更新SeekBar Thumb位置。SeekBar没有更新Android
这是我的代码:
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代码更新搜索栏
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>
'handler.postDelayed(updateTimeProgressBar,1000);'只在1000ms后执行一次,所以进度条会被更新一次。为什么不创建一个每秒运行一次的“计时器”来更新进度条? – 2015-03-19 12:25:35
实际上这是工作,我认为它没有工作,它只被调用一次,所以我添加了日志,并按预期工作,每1秒更新seekbar进度。但是seekbar不动,没有任何事情发生 – 2015-03-19 14:15:19
另外,'处理程序。postDelayed(这个,1000);'跑一次。您可以创建此“处理程序”,以便每秒运行一次。 – 2015-03-19 14:49:34