我正在使用管理搜索栏的 Android 服务。这是代码(我只是放了有关搜索栏的代码):
public class MediaPlayerService extends Service implements
Runnable,
SeekBar.OnSeekBarChangeListener {
private MediaPlayer mediaPlayer = null;
private AudioManager audioManager;
private SeekBar seekBarTime;
@Override
public void onCreate() {
//SET SEEKBAR
LayoutInflater inflater = ( LayoutInflater ) getSystemService( LAYOUT_INFLATER_SERVICE );
View layout = inflater.inflate( R.layout.play_song , null);
seekBarTime = (SeekBar) layout.findViewById(R.id.seekBarTime);
seekBarTime.setOnSeekBarChangeListener(this);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
if(mediaPlayer != null) {
int currentPosition = 0;
int total = mediaPlayer.getDuration();
seekBarTime.setMax(total);
while (currentPosition < total && mediaPlayer != null ) {
try {
Thread.sleep(1000);
currentPosition = mediaPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBarTime.setProgress(currentPosition);
}
}
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.w("disgustingapps", "seekbar");
if (fromUser == true) {
mediaPlayer.seekTo(progress);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
问题有两个。我将尝试用我糟糕的英语来解释这一点:1)我希望服务自动更新搜索栏以显示歌曲的当前时间( mediaPlayer.getCurrentPosition() )2)我希望用户被允许移动搜索栏以便在歌曲的时间之间移动 这两件事都不起作用。似乎 seekbar 与代码完全“独立”。我的意思是:光标根本不移动,如果用户移动它,歌曲会继续播放,什么也不做。请帮助我提前谢谢你