Hi, I'm doing a music player in android studio and I would like to know how I can move to the next song by clicking on the Next button, the same thing in the case when I go back
I'm doing it with a listview and when I click on a song this is played, now that's if the problem works for me with the next and previous buttons do not work as they should.
These songs are in my raw folder
Here is my code are my variables and my onCreate method
ListView listaCanciones;
List<String> list;
ListAdapter adapter;
MediaPlayer mp;
int posicion = 0;
Button play_pause, btn_repetir;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explorador);
play_pause = (Button)findViewById(R.id.btnPlay_Pause);
listaCanciones = findViewById(R.id.lv);
list = new ArrayList<>();
Field[] fields = R.raw.class.getFields();
for (int i = 0; i < fields.length; i++){
list.add(fields[i].getName());
}
adapter = new ArrayAdapter<>(this, R.layout.list_view_configuracion, list);
listaCanciones.setAdapter(adapter);
listaCanciones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(mp != null ){
mp.stop();
mp.release();
}
int resID = getResources().getIdentifier(list.get(i), "raw", getPackageName());
mp = MediaPlayer.create(Explorador.this, resID);
mp.start();
play_pause.setBackgroundResource(R.drawable.pausa);
//Toast.makeText(getApplicationContext(), "Reproduciendo", Toast.LENGTH_SHORT).show();
}
});
}
This is my Play and pause method that works for me correctly
//Metodo para el boton PlayPause
public void play_pause(View view){
if (mp.isPlaying()){
mp.pause();
play_pause.setBackgroundResource(R.drawable.reproducir);
Toast.makeText(this, "Pausa", Toast.LENGTH_SHORT).show();
}
else {
mp.start();
play_pause.setBackgroundResource(R.drawable.pausa);
Toast.makeText(this, "Reproduciendo", Toast.LENGTH_SHORT).show();
}
}
And these are my next and previous methods
//Metodo para boton Siguiente
public void Siguiente(View view){
if (posicion < list.size() -1){
if(mp.isPlaying()){
mp.stop();
posicion++;
int resID = getResources().getIdentifier(list.get(posicion), "raw", getPackageName());
mp = MediaPlayer.create(Explorador.this, resID);
mp.start();
}
else {
posicion++;
}
}
else {
Toast.makeText(this, "No hay más canciones", Toast.LENGTH_SHORT).show();
}
}
//Metodo para boton Anterior
public void Anterior(View view){
if (posicion >= 1){
if (mp.isPlaying()){
mp.stop();
posicion--;
int resID = getResources().getIdentifier(list.get(posicion), "raw", getPackageName());
mp = MediaPlayer.create(Explorador.this, resID);
mp.start();
}else{
posicion--;
}
}
else{
Toast.makeText(this, "No hay más canciones", Toast.LENGTH_SHORT).show();
}
}