How To Get Seekbar To Automatically Move On Song Play?
Solution 1:
First of all you should define a Runnable object that will be triggered each second. For your situation, in everysecond that class will be triggered.
I will paste some example code. Here is the runnable class.
Runnable timerRunnable = new Runnable() {
publicvoidrun() {
// Get mediaplayer time and set the value // This will trigger itself every one second.
updateHandler.postDelayed(this, 1000);
}
};
And you should also have a Handler that will trigger Runnable instance.
HandlerupdateHandler=newHandler();
updateHandler.postDelayed(timerRunnable, 1000);
I hope that sample will help you.
Solution 2:
I am using a Runnable and it looks like this:
private Runnable mUpdateTimeTask = new Runnable()
{
publicvoidrun()
{
long currentDuration = mediaPlayer.getCurrentPosition();
long elapsedDuration = mediaPlayer.getDuration() - currentDuration;
// Displaying current song progress// playing
tvProgressLeft.setText("" + utils.milliSecondsToTimer(currentDuration));
// Displaying remaining time
tvProgressRight.setText("" + utils.milliSecondsToTimer(elapsedDuration));
// Updating progress barint progress = (int) (utils.getProgressPercentage(currentDuration,
elapsedDuration));
// Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);
// Running this thread after 100// milliseconds
handler.postDelayed(this, 100);
}
};
/***/But better yet here is a link to the sample that I have adopted.
http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
Regarding the tutorial the only thing that will not work is retrieving and filtering the song from the given file path. Somehow I couldn't get it to work. But I found another solution which was to write your own FileFilter and use it instead of the default File Filter.
Post a Comment for "How To Get Seekbar To Automatically Move On Song Play?"