I\'ve built a simple music player in Android. The view for each song contains a SeekBar, implemented like this:
public class Song extends Activity implement
I was working with a class that did not contain a reference to the context. So it was not possible for me to use runOnUIThread();
I used view.post();
and it was solved.
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final int currentPosition = mediaPlayer.getCurrentPosition();
audioMessage.seekBar.setProgress(currentPosition / 1000);
audioMessage.tvPlayDuration.post(new Runnable() {
@Override
public void run() {
audioMessage.tvPlayDuration.setText(ChatDateTimeFormatter.getDuration(currentPosition));
}
});
}
}, 0, 1000);
Kotlin Answer
We have to use UI Thread for the job with true way. We can use UI Thread in Kotlin:
runOnUiThread(Runnable {
//TODO: Your job is here..!
})
@canerkaseler
This happened to my when I called for an UI change from a doInBackground
from Asynctask
instead of using onPostExecute
.
Dealing with the UI in onPostExecute
solved my problem.
When using AsyncTask Update the UI in onPostExecute method
@Override
protected void onPostExecute(String s) {
// Update UI here
}