Reputation: 3412
I'm working with an Android service which is managing a seekbar. Here is the code (I just put the code concerning the seekbar):
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
}
}
The problems are two. I will try to explain that with my bad English: 1) I want the seekbar to be automatically updated by the service in order to show the current time of the song ( mediaPlayer.getCurrentPosition() ) 2) I want the user to be allowed to move the seekbar in order to move among the time of the song Both things don't work. It seems that seekbar is completely "indipendent" from the code. I mean: the cursor doesn't move at all and also if the user move it the song go on without doing nothing. Please, help me Thank you in advance
Upvotes: 0
Views: 1170