The program start by creating a MediaPlayer object together with the two buttons and the seek bar. The maximum value of the seek bar is set to the length of the song.
For the Media Player to respond, setOnSeekBarChangeListener was address to the seek bar and a setOnClickListener was added to the buttons.
One of the problems that we encountered in the setOnSeekBarChangeListener was that the listener on the seek bar was not responding to the user changes. The solution was to use the boolean variable fromUser to check if the action is done by the user or not.
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser)
musicService.mp.seekTo(seekBar.getProgress());
}
});
Another problem that we faced was that we could not manage to make the seek bar work without locking the user interface. The solution was to use a handler to post a delay on a runnable object Runnable r = new Runnable() that causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (stopped) {
seekBar.setMax(musicService.mp.getDuration());
handler.postDelayed(r, 500);
stopped = false;
}
if (musicService.isPlaying()) {
musicService.playPause();
playButton.setText("Play");
} else {
musicService.playPause();
playButton.setText("Pause");
}
}
});
After you write bindService(i, servConn, Context.BIND_AUTO_CREATE); android takes some time to process the binding request and you can not use the Service Object immediately after the bindService() method callDownload link: http://ge.tt/4LDRTtf/v/0
No comments:
Post a Comment