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;
}
}
答案 0 :(得分:1)
使用如下代码:
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
玩:
mediaPlayer.start();
暂停:
mediaPlayer.pause();
答案 1 :(得分:1)
您的代码的问题在于您声明了一个未填充的单独的原始资源数组。
int[] resID = new int[0];
mp = MediaPlayer.create(this, resID[songIndex]);
<强>解决方案强>
由于您使用arraylist填充ListView,因此需要从Arraylist本身获取资源。解决方案将是这样的:
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 :(得分:0)
您需要处理资源文件,您可以创建另一种填充歌曲列表的方法,问题来自您使用空数组的int[] resID = new int[0];
。
<强> ADD 强>
int[] songs={R.raw.gemidaoremix,R.raw.nuncanemvi};
使用songs
作为播放列表
<强> 更新 强>
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();
}
}