Reputation: 339
I have a little application that streams from differents urls from the net, I would like to handle invalid/broken url, I have tried the various catch offered but even when streaming a broken file, I can see my log in the log, all I see is
Error (1,-1004)
here is my code for the moment
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(urlString);
Log.w("myApp", "0");
mediaPlayer.prepareAsync(); // might take long! (for buffering, etc)
playPauseButton.setVisibility(View.INVISIBLE);
progressBar1.setVisibility(View.VISIBLE);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
duration = mediaPlayer.getDuration();
textDuration.setText(getDurationString(duration/1000));
progress.setMax(duration);
playPauseButton.setChecked(true);
mediaPlayer.start();
playPauseButton.setVisibility(View.VISIBLE);
progressBar1.setVisibility(View.INVISIBLE);
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.w("myApp", "1");
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
Log.w("myApp", "2");
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Log.w("myApp", "3");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.w("myApp", "4");
e.printStackTrace();
}
Thank you for the help .)
Upvotes: 2
Views: 1427
Reputation: 81
This may help. If you are using Android Studio then watch the Run logs when you are debugging and it will show the error that is thrown.
e.g D/MediaPlayerNative: Message: MEDIA_ERROR(100), ext1=Unknown MediaErrorType(-38), ext2=0x0
This is the nearest I have got to seeing whether a stream is online or not. If you know better please respond.
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Toast.makeText(MainActivity.this, "Media Error", Toast.LENGTH_SHORT).show();
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Toast.makeText(MainActivity.this, "Radio Server Died", Toast.LENGTH_SHORT).show();
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
Toast.makeText(MainActivity.this, "Stream is possibly offline", Toast.LENGTH_LONG).show();
button_stop.performClick();
break;
case MediaPlayer.MEDIA_ERROR_IO:
Toast.makeText(MainActivity.this, "IO Error", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
Upvotes: 1
Reputation: 775
MediaPlayer.OnErrorListener may help. it's not a exception.so you can't catch it.
onError(MediaPlayer mp, int what, int extra);
official doc. MEDIA_ERROR_IO is extra code.
public static final int MEDIA_ERROR_IO
Added in API level 17
File or network related operation errors.
Constant Value: -1004 (0xfffffc14)
Upvotes: 1