0

My app has a RecyclerView in which I will display an audio player.I want to show the current audio time as the audio is played, but when playing play android gives the following error:

E/AndroidRuntime: FATAL EXCEPTION: Thread-4
    Process: myapp.myapp.com.myapp, PID: 24800
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7154)
        at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1128)
        at android.view.ViewGroup.invalidateChild(ViewGroup.java:5286)
        at android.view.View.invalidateInternal(View.java:13616)
        at android.view.View.invalidate(View.java:13580)
        at android.view.View.invalidate(View.java:13564)
        at android.widget.TextView.checkForRelayout(TextView.java:7616)
        at android.widget.TextView.setText(TextView.java:4730)
        at android.widget.TextView.setText(TextView.java:4587)
        at android.widget.TextView.setText(TextView.java:4542)
        at myapp.myapp.com.myapp.adapter.MyAdapter$12$1.run(MyAdapter.java:882)
        at java.lang.Thread.run(Thread.java:761)

Methods inside onBindViewHolder:

    private void reproduzirAudio(MyViewHolder h, File arquivoAudio) {

        h.playAudioChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Criar player
                if (h.mediaPlayer == null) {
                    h.mediaPlayer = MediaPlayer.create(context, Uri.fromFile(arquivoAudio));
                    h.mediaPlayer.setLooping(false);
                    h.mediaPlayer.seekTo(0);

                    //Configurar playButton
                    if (!h.mediaPlayer.isPlaying()) {
                        //stopping
                        h.mediaPlayer.start();
                        h.playAudioChat.setImageResource(R.drawable.ic_pause_circle);
                    } else {
                        h.mediaPlayer.pause();
                        h.playAudioChat.setImageResource(R.drawable.ic_play_circle_white);
                    }

                    //Audiowave
                    h.audioThread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (h.mediaPlayer != null){
                                if (h.mediaPlayer.isPlaying()) {
                                    try {

                                        //Audiowave
                                        float waveProgress = h.mediaPlayer.getCurrentPosition()
                                                / (float) h.mediaPlayer.getDuration() * 100F;
                                        h.audioWaveView.setProgress(waveProgress);
                                        Log.i("WAVE",String.valueOf(waveProgress));

                                        //Timer
                                        h.timeAudioChat.setText(formatarTempoAudio(h.mediaPlayer.getCurrentPosition()));

                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    });
                    h.audioThread.start();
//...
                }
            }
        });
    }
    private String formatarTempoAudio(long currentPosition) {
        String finalTimerString = "";
        String secondsString = "";
        int hours = (((int) currentPosition / (1000 * 60 * 60)));
        int minutes = ((int) currentPosition % (1000 * 60 * 60)) / (1000 * 60);
        int seconds = (((int) currentPosition % (1000 * 60 * 60)) % (1000 * 60) / 1000);
        // Add hours if there
        if (hours > 0) {
            finalTimerString = hours + ":";
        }
        if (seconds < 10) {
            secondsString = "0" + seconds;
        } else {
            secondsString = "" + seconds;
        }
        finalTimerString = finalTimerString + minutes + ":" + secondsString;
        return finalTimerString;
    }

Error runs on line h.timeAudioChat.setText (formatTempoAudio(h.mediaPlayer.getCurrentPosition ()));

I believe this update should be done in the UI Thread, but since I'm on a RecyclerViewAdapter I can't access it. How can I update TextView with the current audio time? Thanks in advance!

Vitor Ferreira
  • 1,075
  • 1
  • 14
  • 28

1 Answers1

0

Despite the organization of your code, which is way improvable, as you have a reference of a context inside the ViewHolder parameter, you can run inside the UI thread this way:

Handler handlerInMainThread = new Handler(itemView.getContext().getMainLooper());

Runnable yourRunnable = new Runnable() {
    @Override 
    public void run() {
        // Put the desired UI code here
    }
 }

handlerInMainThread.post(yourRunnable);

This is a nice answer with a similar problem, and this covers different ways of accessing to UI. There is also documentation in the android docs.

saulmm
  • 2,398
  • 15
  • 21