Skip to main content
2 de 2
adicionou 197 caracteres ao conteúdo

alterar listview play no audio

Gostaria de mudar esse esquema de listview

ao inves de aperter nos botoes play, quero somente apertar no item da lista e ele (no caso) reproduzir o som e quando eu clicar em outro item ele parar o q estava executando e reproduzir o outro, mas estou com algumas dificuldades, resumidamente Gostaria de so clicar no item e ele ja começar a tocar.

se alguem souber algum tutorial tbm sera muito útil obrigado.

CustomMusicAdapter.java

public class CustomMusicAdapter extends BaseAdapter {

private Context context;
private int layout;
private ArrayList<Music> arrayList;
private MediaPlayer mediaPlayer;
private Boolean flag = true;

public CustomMusicAdapter(Context context, int layout, ArrayList<Music> arrayList) {
    this.context = context;
    this.layout = layout;
    this.arrayList = arrayList;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

private class ViewHolder {
    TextView txtName, txtSinger;
    ImageView ivPlay, ivStop;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if(convertView == null){
        viewHolder = new ViewHolder();
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = layoutInflater.inflate(layout, null);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.txtSinger = (TextView) convertView.findViewById(R.id.txtSinger);
        viewHolder.ivPlay = (ImageView) convertView.findViewById(R.id.ivPlay);
        viewHolder.ivStop = (ImageView) convertView.findViewById(R.id.ivStop);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    final Music music = arrayList.get(position);

    viewHolder.txtName.setText(music.getName());
    viewHolder.txtSinger.setText(music.getSinger());

    // play music
    viewHolder.ivPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(flag){
                mediaPlayer = MediaPlayer.create(context, music.getSong());
                flag = false;
            }
            if(mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                viewHolder.ivPlay.setImageResource(R.drawable.ic_play);
            } else {
                mediaPlayer.start();
                viewHolder.ivPlay.setImageResource(R.drawable.ic_pause);
            }
        }
    });

    // stop
    viewHolder.ivStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!flag) {
                mediaPlayer.stop();
                mediaPlayer.release();
                flag = true;
            }
            viewHolder.ivPlay.setImageResource(R.drawable.ic_play);
        }
    });

    return convertView;
}
}

Music.java

public class Music {
private String name;
private String singer;
private int song;

public Music(String name, String singer, int song) {
    this.name = name;
    this.singer = singer;
    this.song = song;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSinger() {
    return singer;
}

public void setSinger(String singer) {
    this.singer = singer;
}

public int getSong() {
    return song;
}

public void setSong(int song) {
    this.song = song;
}
}

CustomMusicAdapter.java

public class MainActivity extends AppCompatActivity {

private ArrayList<Music> arrayList;
private CustomMusicAdapter adapter;
private ListView songList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    songList = (ListView) findViewById(R.id.songList);
    arrayList = new ArrayList<>();
    arrayList.add(new Music("Cảm giác yêu", "Don Nguyễn", R.raw.a));
    arrayList.add(new Music("Cầu vòng tình yêu", "Ustylez", R.raw.b));
    arrayList.add(new Music("Lời anh chưa thể nói", "Hàn Khởi", R.raw.c));
    arrayList.add(new Music("Tan", "Lương Minh Trang", R.raw.d));

    adapter = new CustomMusicAdapter(this, R.layout.custom_music_item, arrayList);
    songList.setAdapter(adapter);
}
}

print da parte foco que estou com problemas.

Como esta o app, gostaria sem precisar usar os botoes de play, so clicando no item da executa.

Como esta o app, gostaria sem precisar usar os botoes de play, so clicando no item da executa.

-Obrigado