我正在使用管理搜索栏的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与代码完全“独立”。我的意思是:光标根本不移动,如果用户移动它,歌曲就会继续运行而不会做任何事情。 请帮我 提前谢谢
答案 0 :(得分:1)
您无法在Android服务中使用SeekBar(或任何UI控件)
答案 1 :(得分:0)