对于安卓单机版的音乐播放器,实现进度条非常容易,通过MediaPlayer对象直接可以得到播放的进度,但是对于远程控制的音乐播放器来说则不是那么回事了,常用的办法是先远程获得MediaPlayer对应的音乐播放总时间,然后在本地对这个总时间进行处理。那么常用的“暂停/播放”、“快进/快退”等都需要把他们放到线程里作处理。
************************************************************
//程序完整代码
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Media_Player2Activity extends Activity {
/** Called when the activity is first created. */
private SeekBar testBar;
ThreadTest r = null;
Thread t = new Thread(r);
private Button btn1;
private Button btn2;
private Button btn3;
private int isPlay = 0;
private int status=0;
private int operate=-1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testBar = (SeekBar) findViewById(R.id.testBar);
btn1 = (Button) findViewById(R.id.btn1);//点击btn1结算线程并重新开始
btn2 = (Button) findViewById(R.id.btn2);//点击btn2结算线程并重新开始
btn3 = (Button) findViewById(R.id.btn3);//点击btn3暂停/开始线程
btn1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if (!t.isInterrupted()) {
t.interrupt();
if (r != null) {
r.shutDown();
r = null;
}
t = null;
}
r = new ThreadTest(1);
t = new Thread(r);
t.start();
for (int i = 0; i < 100; i++) {
Log.v("i", i + "");
}
}
}
);
btn2.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if (!t.isInterrupted()) {
t.interrupt();
if (r != null) {
r.shutDown();
r = null;
}
t = null;
}
r = new ThreadTest(1);
t = new Thread(r);
t.start();
}
}
);
btn3.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if (isPlay == 0) {
if(t!=null)
{
if (!t.isInterrupted()) {
t.interrupt();
if (r != null) {
r.shutDown();
r = null;
}
t = null;
}
}
isPlay=1;
} else {
if(t!=null)
{
if (!t.isInterrupted()) {
t.interrupt();
if (r != null) {
r.shutDown();
r = null;
}
t = null;
}
}
r = new ThreadTest(0);
t = new Thread(r);
t.start();
isPlay=0;
}
}
}
);
}
class ThreadTest implements Runnable {
private boolean flag = true;
int i = 0;
public ThreadTest(int operate)
{
if(operate==1)
{
//根据operate判断进度条是继续执行还是重新开始
status=0;
}
}
public void run() {
// TODO Auto-generated method stub
while (flag) {
try {
testBar.setProgress(status);
try {
testBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser) {
Log.v("this is from user",
"this is from user");
status=seekBar.getProgress();
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.v("onStartTrackingTouch",
"onStartTrackingTouch");
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.v("seekBar.getProgress:",
seekBar.getProgress() + "");
i = seekBar.getProgress();
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
status++;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void shutDown() {
Log.v("has stopped", "has stopped");
flag = false;
}
}
}
************************************************************
//xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btn1" android:text="按钮1" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btn2" android:text="按钮2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btn3" android:text="按钮3" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<SeekBar android:id="@+id/testBar" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>