1

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);

                }
            });

        }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Bence
  • 109
  • 2
  • 11
  • Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561), [How do I avoid misusing tags?](https://meta.stackoverflow.com/q/354427/6296561), and [the tagging guide](/help/tagging). Use [android] or other relevant tags instead. – Zoe Dec 25 '20 at 12:46

1 Answers1

0

Here the xml example code where imageview and progress bar are in center of its parent relative layout, you could change its view as you like.

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ProgressBar
            android:id="@+id/progress"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginBottom="50dp"
            android:layout_centerInParent="true" />
        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:contentDescription="@string/todo" />

    </RelativeLayout>

When your video is done with loading you can hide the progress bar like this.

 viewHolder.progressBar.setVisibility(View.GONE);

ahmad bajwa
  • 966
  • 2
  • 10
  • 30
  • Thanks for the answer but this is not what I'm looking for. I have already set up the progress bar in the layout file. I want to sync it with the MediaPlayer inside the RecyclerView's adapter class. – Bence Dec 24 '20 at 12:42
  • add this xml in your recyclerview row. I am using this in my own recyclerview with imageview. – ahmad bajwa Dec 24 '20 at 12:47
  • But my problem is not that the progress bar is not showing in the item's layout. The problem is that it is not showing progress in sync with the MediaPlayer. And I don't want to set it's visibility to gone neither. – Bence Dec 24 '20 at 12:50
  • I have a style="@style/Widget.AppCompat.ProgressBar.Horizontal" progress bar – Bence Dec 24 '20 at 12:51
  • Ok. I am giving you a suggestion to use that way, – ahmad bajwa Dec 24 '20 at 12:52