Hello how are you? I'm trying to make a listview of audios everything seems to flow as it should, but when I click on a listitem the audio should reproduce but instead it appears "the app stopped working" I do not understand what's wrong with my code someone can help me I'm at times breaking my head but I can not solve this problem. Below is the logcat, and then my code, please someone help me make it work. -Thank you
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memes_main);
lv = findViewById(R.id.lv);
mp = new MediaPlayer();
ArrayList<memes> item = new ArrayList<>();
item.add(new memes("Gemidão", R.raw.gemidaoremix));
item.add(new memes("Nunca nem vi", R.raw.nuncanemvi));
ArrayAdapter<memes> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(adapter);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
playSong(position);
}
});
}
public void playSong(int songIndex) {
mp.reset();
int[] resID = new int[0];
mp = MediaPlayer.create(this, resID[songIndex]);
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
public class memes{
private String nome;
private int resID;
memes(String nome, int resID){
this.nome = nome;
this.resID = resID;
}
public String getNome(){
return nome;
}
public int getResId(){
return resID;
}
@Override
public String toString(){
return nome;
}
}
The problem with your code is that you are declaring a separate array of raw resource which is not populated.
int[] resID = new int[0];
mp = MediaPlayer.create(this, resID[songIndex]);
Solution
Since you are using arraylist to populate the ListView,you need to fetch the resource from the Arraylist itself. The solution will be like this :
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
ArrayList<Memes> item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.lv);
mp = new MediaPlayer();
item = new ArrayList<>();
item.add(new Memes("Gemidão", R.raw.gemidaoremix));
item.add(new Memes("Nunca nem vi", R.raw.nuncanemvi));
ArrayAdapter<Memes> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(adapter);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
playSong(position);
}
});
}
public void playSong(int songIndex) {
mp.reset();
mp = MediaPlayer.create(this, item.get(songIndex).getResId());
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}