Good I need to disable the buttons of my application of a radio until this one executes the stream of music, and then that they work with normality since if I touch a button before initiating the stream it closes. Try with an if and boton.setEnabled
but then the buttons do not work even after starting the radio. Thank you very much already.
Java code:
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()));
}
}
}
}