Edición
Buscando como solventar los cortes de audio he encontrado la recomendación de Google en usar ExoPlayer para consumir broadcast streaming de audio, ofrece una mayor configuración...
Aquí el Github de ExoPlayer
Otras librerías
RadioPlayService
OpenPlayer Android
Media Player
Acabo de hacer una prueba y veo que el MediaPlayer
no pasa por el onBufferingUpdate
no ser si es porque es un streaming de radio, su configuración etc... en cambio si se prueba con un mp3 entero si que pasa por el onBufferingUpdate
- Cuando se corta el streaming se recibe un
E/MediaPlayer: error (-38, 0) o bien un waring W/MediaPlayer: info/warning (702, 0)
com.webserveis.app.testaudiostream E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
com.webserveis.app.testaudiostream E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
com.webserveis.app.testaudiostream E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
com.webserveis.app.testaudiostream E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
com.webserveis.app.testaudiostream E/MediaPlayer: Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=1
com.webserveis.app.testaudiostream E/MediaPlayer: error (-38, 0)
com.webserveis.app.testaudiostream E/MediaPlayer: Error (-38,0)
El ejemplo tiene algunos bugs, pero es funcional:
package com.webserveis.app.testaudiostream;
import android.app.ProgressDialog;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
/*
https://www.sitepoint.com/develop-music-streaming-android-app/
*/
public class Main2Activity extends AppCompatActivity implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener,MediaPlayer.OnSeekCompleteListener {
private String TAG = MainActivity.class.getSimpleName();
private Button btn;
private boolean playON;
private MediaPlayer mediaPlayer;
private ProgressDialog progressDialog;
private boolean initialStage = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btn = (Button) findViewById(R.id.audioPlayPause);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnCompletionListener(this);
progressDialog = new ProgressDialog(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!playON) {
btn.setText("Pause Streaming");
if (isNetworkAvailable()) {
if (initialStage) {
//String sourceAudio = "https://www.ssaurel.com/tmp/mymusic.mp3"; //normal
String sourceAudio = "http://www.radionoventa.com.ar:8000/Noventa_en_vivo.mp3"; //streaming
try {
mediaPlayer.setDataSource(sourceAudio);
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
} else {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
Log.i(TAG, "continue playing ");
}
}
playON = true;
} else {
Log.w(TAG, "onClick: No internet connection");
}
} else {
btn.setText("Launch Streaming");
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
playON = false;
}
}
});
}
private void togglePlayPause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
@Override
protected void onDestroy() {
Log.w(TAG, "onDestroy: ");
super.onDestroy();
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
Log.w(TAG, "stop mediaplayer: ");
mediaPlayer.stop();
}
Log.w(TAG, "remove mediaplayer: ");
mediaPlayer.release();
mediaPlayer = null;
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
Log.i(TAG, "onBufferingUpdate() called with: mediaPlayer = [" + mediaPlayer + "], i = [" + i + "]");
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.i(TAG, "onCompletion() called with: mediaPlayer = [" + mediaPlayer + "]");
Log.e(TAG, "SE HA CORTADO EL STREAMING o buffer vacio");
}
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
Log.w(TAG, "onError() called with: mediaPlayer = [" + mediaPlayer + "], i = [" + i + "], i1 = [" + i1 + "]");
return false;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG, "onPrepared() called with: mediaPlayer = [" + mediaPlayer + "]");
mediaPlayer.start();
initialStage = false;
}
@Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
Log.d(TAG, "onSeekComplete() called with: mediaPlayer = [" + mediaPlayer + "]");
}
}