Android 开发之音乐播放之进度条
2010-04-12 19:05:00 来源:WEB开发网// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int dest = seekBar.getProgress();
int mMax = mPlayer.getDuration();
int sMax = sBar.getMax();
mPlayer.seekTo(mMax*dest/sMax);
}
});
4. 刷新播放位置 且使其实时变化
//因为MediaPlayer没有播放进度的回调函数 所以只能:开辟一个Thread 定时通知其刷新
Java代码
1. public void startProgressUpdate(){
2. //开辟Thread 用于定期刷新SeekBar
3. DelayThread dThread = new DelayThread(100);
4. dThread.start();
5. }
public void startProgressUpdate(){
//开辟Thread 用于定期刷新SeekBar
DelayThread dThread = new DelayThread(100);
dThread.start();
}
而该Thread 具体实现为:
Java代码
1. private Handler mHandle = new Handler(){
2. @Override
3. public void handleMessage(Message msg){
4. int position = mPlayer.getCurrentPosition();
5.
6. int mMax = mPlayer.getDuration();
7. int sMax = sBar.getMax();
8.
9. sBar.setProgress(position*sMax/mMax);
10. }
11. };
12.
13. public class DelayThread extends Thread {
14. int milliseconds;
15.
16. public DelayThread(int i){
17. milliseconds = i;
18. }
19. public void run() {
20. while(true){
21. try {
22. sleep(milliseconds);
23. } catch (InterruptedException e) {
24. // TODO Auto-generated catch block
25. e.printStackTrace();
26. }
27.
28. mHandle.sendEmptyMessage(0);
29. }
30. }
31. }
5. emulator 运行截图:
更多精彩
赞助商链接