Android 播放器开发之seekBar
2010-11-05 00:55:44 来源:WEB开发网1.在播放器上加入滚动条的代码如下,把goOn()放到onCreate()里:
Java代码
//进度条
public void goOn()
{
seekBar.setMax(mediaPlayer.getDuration());
new Thread(new Runnable(){
public void run() {
while(true){
//获得当前播放的进度值
seekBar.setProgress(mediaPlayer.getCurrentPosition());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}).start();
}
//进度条
public void goOn()
{
seekBar.setMax(mediaPlayer.getDuration());
new Thread(new Runnable(){
public void run() {
while(true){
//获得当前播放的进度值
seekBar.setProgress(mediaPlayer.getCurrentPosition());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}).start();
}
2.滚动条拖动播放的代码如下:将startProgressUpdate()方法放入onCreate()里面
Java代码
//拖动进度条更新
public void startProgressUpdate(){
//开辟Thread 用于定期刷新SeekBar
DelayThread dThread = new DelayThread(100);
dThread.start();
}
private Handler mHandle = new Handler(){
@Override
public void handleMessage(Message msg){
int position = mediaPlayer.getCurrentPosition();
int mMax = mediaPlayer.getDuration();
int sMax = seekBar.getMax();
seekBar.setProgress(position*sMax/mMax);
}
};
public class DelayThread extends Thread {
int milliseconds;
public DelayThread(int i){
milliseconds = i;
}
public void run() {
更多精彩
赞助商链接