change listview play in audio

Asked

Viewed 64 times

1

I’d like to change that listview scheme

instead of pressing the play buttons, I just want to press the list item and it (in case) plays the sound and when I click on another item it stops the q was running and play the other one, but I’m having some difficulties, briefly I would like to just click on the item and it already start playing.

If anyone knows any tutorial tbm will be very helpful thank you.

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.

Like this app, would like without using the play buttons, just by clicking on the run item.

-Thank you

1 answer

0

Within getView in CustomMusicAdapter.java insert the following code below to give action to 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();           

    }
});
  • viewHolder.itemView.setOnClickListener(new View.OnClickListener() {&#xA;@Override&#xA;public void onClick(View v) {&#xA;if(flag){&#xA;mediaPlayer = MediaPlayer.create(context, music.getSong());&#xA;flag = false;&#xA; } else {&#xA;mediaPlayer.start();&#xA;} Stopped working when I use this

  • @Welyson did not understand. You got some doubt?

  • nor do I just want that when I click play the audio, but I can’t find all kinds of tutorial on yt but none specifically. what I want to do

  • @Welyson edited the answer to play the audio.

  • does not give the app to work, this is the project link (https://github.com/quocnguyenvan/media-player-demo)

  • @Welyson you have the audio files inside the raw in your project? What error appears in your logcat?

  • I got, thank you very much worked&#A;porem the project scheme ta to play when you click on the play button of each item as in the app print, but I would like to play when you click on the item itself, I believe that in the case there is something related to the "itemView" q ta missing change somewhere, but his help was a hand on the wheel vlw msm viewHolder.ivPlay.setOnClickListener(new View.OnClickListener() ivPlay would be the boot on the items when placing itemView does not work.

  • then, the doubt is where else I change the itemView, to reproduce in the item and not in the play button(ivPlay)

  • You can even take a test by deleting the ivPlay and ivStop buttons as they won’t get any more action. By clicking viewHolder.itemView that would be in the item, it is already to play.

  • and how is the code to stop an audio when I start running the other one? -thanks

  • I tried to use onDestroy, to stop the previous audio, but it worked, how do I stop, I am using listview

Show 6 more comments

Browser other questions tagged

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