Acceder

Ver la Versión Completa : [ CONSULTA ] Problema con reproductor mp3 streaming dentro del Navigation Drawer


ruben_ups
03/07/14, 02:59:01
Buenas noches.

La verdad que nunca habia creado un post en ninguna pagina, porque siempre intento buscarme la vida por mi cuenta hasta hoy xD.

Bueno de primeras, muchas gracias a todos por vuestro tiempo y ayuda.

Mi problema es que al abrir con el Navigation Drawer una actividad, no puedo hacer uso de los botones Play Stop definidos en el layout... Simplemente lo unico que consigo es que funcione el streaming sin hacer uso de los botones.. pero vaya eso no interesa..

(Ahora no funcionara el streaming porque he apagado el reproductor) pero lo podeis probar con cualquier otro.. por ejemplo este http://www.codigofuenteya.com.ar/recursos/gato.mp3 )

Os pego un poco de cogido..



public class StreamingFragment extends Fragment {

public StreamingFragment(){}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.streaming, container, false);

return rootView;
}






static MediaPlayer mPlayer;
Button buttonPlay;
Button buttonStop;
String url = "http://78.129.190.50/listen.php?ip=78.129.190.50&port=39150";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUS IC);
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();
}
});

buttonStop = (Button) 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();
}
}
});
}

protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

}




:gracias:

ruben_ups
03/07/14, 03:10:39
private void MostrarFragment(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new ProfileFragment();
break;
case 3:
fragment = new StreamingFragment();
break;


default:
//si no esta la opcion mostrara un toast y nos mandara a Home
Toast.makeText(getApplicationContext(),"Opcion "+titulos[position-1]+"no disponible!", Toast.LENGTH_SHORT).show();
fragment = new HomeFragment();
position=1;
break;
}

Arasthel
03/07/14, 11:24:38
Varias cosas.

Por favor, la próxima vez usa los bloques de código con '[ CODE]' (sin el espacio)

Cuando quieras poner código y asegúrate de que mantiene la indentación. Si no, cuesta mucho más leerlo y entenderlo.

Si estás usando un fragment, no sé por qué en otro sitio dices que es una Activity o si son 2. Si son 2 explica mejor qué hace cada uno, porque no entiendo el StreamingFragment por un lado y la 2º parte del código que imagino será el de una Activity que es la que tiene el MediaPlayer.

En el código además veo un problema de asincronía. Si vas a pedirle al MediaPlayer que se prepare en el Main Thread, mejor usa prepareAsync() y lanza el mPlayer.start() cuando salte el onPreparedListener(). Este último tendrías que ponerlo con un mPlayer.setOnPreparedListener(). De otra forma se bloqueará el hilo principal hasta que se haya cargado suficientes datos en el buffer.

La cosa es que MediaPlayer tira de NDK por detrás y el tratamiento de errores es bastante malo, se traduce en "si me dices que haga algo y no puedo, quedo inservible", teniendo que prepararlo de nuevo y demás.

Un saludo.

Arasthel
03/07/14, 11:26:16
Espérate. No estarás intentando usar views del fragment desde el Activity con el findViewById(), ¿no? No es buena idea (no sé ni si se puede hacer) y si haces el findViewById en el onCreate aún menos, porque en ese momento ni siquiera estará cargado el fragment que quieras y te tomará los View como null.

ruben_ups
03/07/14, 20:38:06
Muchas gracias por tu respuesta!!
No sabia como se ponía código en un foro perdón.

Básicamente lo que quiero hacer es poner un streaming (mp3) con dos botones Play, Stop en un Fragment del Navigation Drawer. Pero sigo sin poder

ruben_ups
05/07/14, 19:42:39
nadie?

Dexafree
06/07/14, 08:56:06
nadie?

Para poder ayudarte mejor seria interesante que respondieras a las preguntas que Arasthel te ha hecho, y comentaras el resultado que te ha dado usar los métodos del MediaPlayer en los lugares que te ha sugerido

ruben_ups
07/07/14, 19:01:15
Perdonad no haber contestado antes.. esque el 10 termino exámenes en la facultad.
Después de esa fecha haré y comentare lo que me ha dicho Arasthel.

Un saludo y gracias