Reputation: 2340
My code plays a song from a listview, everything perfect, BUT, if I specifically rotate it, the media player stop, how can i keep it playing? and! if i create an another button to pause, or something like that, should I use the same answer? my code in a button:
testme.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(nombre.matches("Kaminomi")) { //ignore this
try {
mp.setDataSource(nombreSong);
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
if (validar == false) {
validar = true;
mp.start();
Toast.makeText(getApplicationContext(), "Reproduciendo ahora: \n" + nombreSong, Toast.LENGTH_LONG).show();
testme.setText("Stop :D");
testme.setCompoundDrawablesWithIntrinsicBounds(R.drawable.nisekoi1,0,0,0);
} else {
validar = false;
Toast.makeText(getApplicationContext(), "Deteniendo: \n" + nombreSong, Toast.LENGTH_LONG).show();
mp.stop();
testme.setText("Play :D");
}
}else{
Toast.makeText(getApplicationContext(), "Algo mas creo", Toast.LENGTH_LONG).show(); //ignore this too
}
}
Upvotes: 0
Views: 367
Reputation: 1620
Put this in your manifest xml file:
<activity android:name="MainActivity"
android:configChanges="keyboardHidden|orientation">
i.e. add android:configChanges="keyboardHidden|orientation"
to the activity xml element.
Whats happening is that when your screen orientation changes, a configuration change occurs and this causes Android to destroy and recreate your activity.
The above code in my answer will prevent a configuration change from happening when an orientation change or a keyboard change occurs.
However this is just a workaround. A configuration change can occur at any time (e.g. placing the phone in a docking station) and therefore just disabling configuration changes for orientation changes and keyboard changes is not enough.
To properly maintain the state of objects during configuration changes, Fragments are a good approach.
Upvotes: 1