change listview play in audio

1

I would like to change this listview schema

Instead of pushing on the play buttons, I just want to push the list item and it (in the case) play the sound and when I click on another item it stops what was running and play the other but I'm having some difficulties, I would like to just click on the item and it will start playing.

If anyone knows some tutorial, it will be very useful thanks.

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

As the app is, I would like to use the play buttons, just by clicking on the play item.

-Thank you

    
asked by anonymous 30.08.2017 / 05:10

1 answer

0

Within getView in CustomMusicAdapter.java enter the following code below to give action to the click of the item. See:

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

       // aqui dentro você coloca a condição para iniciar
       // e finalizar o áudio. Por exemplo para iniciar, basta
       // usar o código abaixo

       MediaPlayer mediaPlayer = MediaPlayer.create(context, music.getSong());
       mediaPlayer.start();           

    }
});
    
30.08.2017 / 05:18