Thread inside an Adapter (music player list)

Asked

Viewed 39 times

1

Have Recyclerview that has a list of audios, each line has an audio. When I run, I need seekbar to start the progress that refers to the specific line. For that I understand that I need a thread to run progress. my difficulty is that I cannot run thread inside Adapter responsible for the list. why do I have to run on the Adapter? pq it responsible for clicking the play button. I can even run the thread more in an internal Class, but I can’t access the Adapter components inside Uithread(main thread). is there any way to resolve this? helper.

package com.amazongas.paulo.cofferholy.adapter;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.amazongas.paulo.cofferholy.R;
import com.amazongas.paulo.cofferholy.model.Audio;

import java.io.IOException;
import java.util.List;

public class AdapterAudio  extends RecyclerView.Adapter<AdapterAudio.MyViewHolder>{

    private List<Audio> listaAudios;
    private Context context;
    private MediaPlayer player;
    private Runnable runnable;
    private Handler handler;

    public AdapterAudio(Context context, List<Audio> lista) {

        this.listaAudios = lista;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemLista = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.adapter_audio, viewGroup, false);

        return new MyViewHolder(itemLista);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int i) {
        final Audio audio = listaAudios.get(i);
        holder.titulo.setText(audio.getTitulo());
        holder.autor.setText(audio.getAutor());
        holder.data.setText(audio.getData());

        holder.btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(player == null){

                    player = new MediaPlayer();
                    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    try {
                        player.setDataSource("https://www.amazongas.com.br/wp-content/themes/amazongas/Music/faixa01.mp3");
                        player.prepare();
                        holder.seekBar.setMax(player.getDuration());
                        player.start();
                        //changerSeekBar();

                        MyThred myThred = new MyThred();
                        myThred.start();


                        //Toast.makeText(context, audio.getTitulo(), Toast.LENGTH_SHORT).show();

                        String tempo = milliSecondsToTimer(player.getDuration());
                        Toast.makeText(context,"Playback start "+tempo, Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }else{
                    player.start();
                }

            }

        });



    }

    @Override
    public int getItemCount() {
        return listaAudios.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView titulo;
        TextView autor;
        TextView data;
        ImageButton btnPlay;
        SeekBar seekBar;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            titulo = itemView.findViewById(R.id.txtTitulo);
            autor = itemView.findViewById(R.id.txtAutor);
            data = itemView.findViewById(R.id.txtData);
            btnPlay = itemView.findViewById(R.id.btnPlay);
            seekBar = itemView.findViewById(R.id.seekBar);

        }
    }

    public String milliSecondsToTimer(long milliseconds){
        String tempoFinal = "";
        String segundosString;

        //converter millis em tempo.
        int horas = (int) (milliseconds / (1000*60*60));
        int minutos = (int) (milliseconds % (1000*60*60)) / (1000*60);
        int segundos = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);

        if(horas > 0){
            tempoFinal = horas +":";
        }

        if(segundos > 60){
            segundosString = "0" + segundos;
        }else{
            segundosString = "" + segundos;
        }

        tempoFinal = tempoFinal + minutos + ":" + segundosString;

        return tempoFinal;

    }


    class MyThred extends Thread{

        @Override
        public void run() {
            super.run();

            for(int i=0; i<=20; i++){
                Log.d("tempo", "Posicao"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

  • Why do you say you can’t thread inside the Adapter?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.