I have RecyclerView inside a Fragment. The RecyclerView's every item has a ProgressBar. In the itemView's onClick listener a MediaPlayer plays a sound. I want to indicate the MediaPlayer's progress with a ProgressBar, which is inside the same ViewHolder. How can I achieve this?
I have tried to work in the Activity's runOnUiThread, but the ProgressBar stops in the beginning.
@Override
public void onClick(View view) {
if(mMediaPlayer.isPlaying())
{
mMediaPlayer.stop();
}
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(mContext,mSound.getSoundUri());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.start();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
final int duration = mMediaPlayer.getDuration();
final int amoungToupdate = duration / 100;
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
((MainActivity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
if (!(amoungToupdate * mProgressBar.getProgress() >= duration)) {
int p = mProgressBar.getProgress();
p += 1;
mProgressBar.setProgress(p);
}
}
});
};
}, amoungToupdate);
}
});
}