How to create a button for each Listview item that accesses a song

Asked

Viewed 132 times

1

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

I used a listview personalized in the main activity and my adapter is 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;}
  • 1

    has made some code, which language is using?

  • 1

    What do you already have? Your question involves many steps and a complete answer would be unfeasible. Focus on a specific problem.

  • I got the question. Do you know how to fix? The language is android.

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 touch the item change 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 and so forth.

Don’t forget when you stop:

waveOutDevice.Stop();
audioFileReader.Dispose();
waveOutDevice.Dispose();

Browser other questions tagged

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