Buenas necesito inhabilitar los botones de mi aplicación de una radio hasta que este se ejecute el stream de música, y luego que funcionen con normalidad dado que si toco algún botón antes de iniciar el stream se cierra.Intente con un if y boton.setEnabled
pero luego los botones no funcionan inclusive después de iniciar el la radio . Desde ya muchas gracias.
Codigo java:
public class MainActivity extends Activity {
ImageButton btnPlay;
int a;
ImageButton btnRec;
ImageButton btnStp;
ImageButton btnSal;
Button btnMul;
Button btnRa;
Button btnNos;
MediaPlayer mediaplayer;
String stream="http://streams.calmradio.com/api/43/128/stream/;";
boolean prepared,started=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (ImageButton) findViewById(R.id.play);
btnRec = (ImageButton) findViewById(R.id.reconectar);
btnStp = (ImageButton) findViewById(R.id.parar);
btnSal = (ImageButton) findViewById(R.id.salir);
btnMul = (Button) findViewById(R.id.multimedia);
btnRa = (Button) findViewById(R.id.radio);
btnNos = (Button) findViewById(R.id.nosotros);
final Toast toast = Toast.makeText(getApplicationContext(), "Esperando respúesta del Servidor...", Toast.LENGTH_LONG);
toast.show();
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
//botones Radio
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (started){
started=false;
mediaplayer.pause();
btnPlay.setBackgroundResource(R.drawable.play);
Toast toast=Toast.makeText(getApplicationContext(),"Pausa",Toast.LENGTH_SHORT);
toast.show();
}else{
started=true;
mediaplayer.start();
btnPlay.setBackgroundResource(R.drawable.pause);
Toast toast=Toast.makeText(getApplicationContext(),"Play",Toast.LENGTH_SHORT);
toast.show();
}
}
});
btnStp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaplayer.stop();
Toast toast=Toast.makeText(getApplicationContext(),"Stop",Toast.LENGTH_SHORT);
toast.show();
}
});
btnRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaplayer.stop();
Intent Recargar =getIntent();
finish();
startActivity(Recargar );
}
});
//Botones Navegacion
btnSal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaplayer.release();
System.exit(0);
}
});
btnNos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Nosotros=new Intent(getApplicationContext(),Nosotros.class);
startActivity(Nosotros);
mediaplayer.stop();
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Multimedia=new Intent(getApplicationContext(),Multimedia.class);
startActivity(Multimedia);
mediaplayer.stop();
}
});
}
class PlayerTask extends AsyncTask<String,Void,Boolean>{
protected Boolean doInBackground(String...String){
try{
mediaplayer.setDataSource(String[0]);
mediaplayer.prepare();
prepared=true;
}catch(IOException e){
e.printStackTrace();
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean){
super.onPostExecute(aBoolean);
mediaplayer.start();
}
private class BecomingNoisyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction()));
}
}
}
}