如何在Android媒体播放器中使用搜索栏?我使用下面的代码。 seekbar只在第一首歌中工作,然后转离我的应用程序。
mp = MediaPlayer.create(player.this, (Integer) plsong.get(i));
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
mp.release();
sname.setText(" ");
if (j < (plsong.size() - 1)) {
j++;
mp = MediaPlayer.create(player.this,(Integer) plsong.get(j));
sname.setText((CharSequence) soname.get(j));
mp.start();
mp.setOnCompletionListener(this);
}
}
});
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mpss) {
// TODO Auto-generated method stub
total = mp.getDuration();
System.out.println("total" + total);
}
});
seekbar.setProgress(0);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser) {
mp.seekTo(progress);
seekBar.setProgress(progress);
}
}
});
Thread currentThread = new Thread(MyCounter.this);
currentThread.start();
mplay = (Button) findViewById(R.id.mplay);
if(!mp.isPlaying())
{
mplay.setText("Pause");
mplay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
seekbar.setMax(total);
}
});
}
else
{
mplay.setText("play");
mplay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
}
});
}
}
public void run() {
// TODO Auto-generated method stub
try {
while (mp != null) {
int currentPosition = mp.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
}
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Handler threadHandler = new Handler() {
public void handleMessage(Message msg) {
seekbar.setProgress(msg.what);
}
};
答案 0 :(得分:3)
删除您创建的所有线程以移动搜索条并添加以下内容:
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener(){
public void onBufferingUpdate(MediaPlayer mp, int percent) {
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
if (mp.isPlaying() && seekBar!=null) {
seekBar.setProgress(mp.getCurrentPosition());
}
}
});