How to create a button for each item in a ListView that accesses a song

1

I wanted each item of ListView to play a song by clicking the button for each item, but when I click the button, it even plays, only the first song, the other songs in the list do not play.

I used a% custom% in the main activity and my listview looks like this:

public class Adapter extends ArrayAdapter<Itens>{

private MediaPlayer mMedia; 

(...)

final Button playButton = (Button)layout.findViewById(R.id.start);

if (item.hasAudio()){
playButton.setOnClickListener(new View.OnClickListener(){
@Override
públic void onClick (View v){
mMedia = MediaPlayer.create(getContext(),item.getAudio());
mMedia.start();
mMedia.onCompletionListener = (...)}

return Layout;}
    
asked by anonymous 24.11.2017 / 19:27

1 answer

1

In the SelectedIndexChanged event you can play the song by doing the following.

Install the NAudio package (Package Manager Console) - Nuget

  

Install-Package NAudio

If you want a song to play the change of item in your List:

private void lstItens_SelectedIndexChanged(object sender, EventArgs e)
{
    IWavePlayer waveOutDevice = new WaveOut();
    AudioFileReader audioFileReader = new AudioFileReader(@"C:\temp\songs\song1.mp3");

    waveOutDevice.Init(audioFileReader);
    waveOutDevice.Play();
}

So you can increment your code for each item you select a sound file .. and so on.

Do not forget when you give a Stop:

waveOutDevice.Stop();
audioFileReader.Dispose();
waveOutDevice.Dispose();
    
25.11.2017 / 11:26