问题
Listview with audios does not play
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
Logcat
MainActivity.class
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();
}
}
memes.class
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;
}
}
回答1:
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();
}
}
回答2:
Use code like below:
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
to play:
mediaPlayer.start();
to pause:
mediaPlayer.pause();
回答3:
you need to work on the resource files , you can create another method that fills your list of songs , your problem comes from the int[] resID = new int[0];
you are using an empty array.
see also Supported Media Formats
ADD
int[] songs={R.raw.gemidaoremix,R.raw.nuncanemvi};
use songs
as a playlist
UPDATE
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
int[] songs={R.raw.gemidaoremix,R.raw.nuncanemvi};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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();
mp = MediaPlayer.create(this, songs[songIndex]);
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
来源:https://stackoverflow.com/questions/47898076/media-player-does-not-work-in-my-code