Dear good morning, I am quite a novice in the development of mobile applications, I am learning and I need to be able to show a loading after launching my first image of the app in a Splash.
That is, I have an app that launches a Splash, it lasts approximately 2 seconds, while loading a url that contains the streaming that I must play when I start the app, it takes a while to load this url and during that moment I want to show a loading, how can I do it?
This is my final code.
public class MainActivity extends Activity implements OnClickListener {
static MediaPlayer mPlayer;
ImageButton buttonPlay;
ImageButton buttonStop;
String url = "url";
Button btn_politica, btn_deportes, btn_espectaculo, btn_entretencion;
ProgressBar progressbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//analiza el archivo XML, traduce a objetos cada componente,
progressbar = (ProgressBar) findViewById(R.id.mi_loading);
onPrepared(mPlayer);
//le asigna los atributos, establece contenedores y todas las relaciones
//padre e hijo necesarias.
//Bloque de coding para el streaming al cargar el activity
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
//Finaliza bloque de codigo para cargar el streaming al inicio de la aplicacion+
initialize();
//Bloque de codigo para el streaming al presionar play
buttonPlay = (ImageButton) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
// progressbar = (ProgressBar) findViewById(R.id.mi_loading);
//progressbar.setVisibility(View.GONE);
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
// progressbar.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.INVISIBLE);
buttonStop.setVisibility(View.VISIBLE);
}
});
//Bloque de codigo para el streaming al presionar pause
buttonStop = (ImageButton) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
//buttonPlay.setEnabled(true);
buttonPlay.setVisibility(View.VISIBLE);
buttonStop.setVisibility(View.INVISIBLE);
}
}
});
}
public void onPrepared(MediaPlayer mp)
{
progressbar.setVisibility(View.INVISIBLE);
}
private void initialize() {
btn_deportes = (Button) findViewById(R.id.button1);
btn_deportes.setOnClickListener(this);
btn_politica = (Button) findViewById(R.id.button2);
btn_politica.setOnClickListener(this);
btn_espectaculo = (Button) findViewById(R.id.button3);
btn_espectaculo.setOnClickListener(this);
btn_entretencion = (Button) findViewById(R.id.button4);
btn_entretencion.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startActivity(new Intent(this, Activity_Noticias_Deportes.class));
break;
case R.id.button2:
startActivity(new Intent(this, Activity_Noticias_Politica.class));
break;
case R.id.button3:
startActivity(new Intent(this, Activity_Noticias_Economia.class));
break;
case R.id.button4:
startActivity(new Intent(this, Activity_Noticias_Entretencion.class));
break;
}
}
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}