I'm having a mediaplayer app where I fetch my datasource from my server with a link path. The problem is when I was testing, I purposely deleted the datasource file of the audio link path I fed to mp.setDataSource(musicUri);
to check but I can't seem to catch the error and resolve it.
I already replaced my mp.prepare();
to mp.prepareAsync();
and simple mp.start()
to
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
to listen if mediaplayer is prepared before starting it.
This is the stack trace:
07-23 13:55:52.492: E/MediaPlayer(6870): Attempt to call getDuration without a valid mediaplayer
07-23 13:55:52.492: E/MediaPlayer(6870): error (-38, 0)
07-23 13:55:52.492: E/MediaPlayer(6870): Error (-38,0)
07-23 13:55:52.492: E/MediaPlayer(6870): stop called in state 0
07-23 13:55:52.492: E/MediaPlayer(6870): error (-38, 0)
07-23 13:55:52.492: E/MediaPlayer(6870): error (1, -107)
Nothing seems to work, I would like to catch this error to fix the abnormal results it would cause to the mediaplayer. Any help will do. Thanks!
EDIT:
Okay, so I added onErrorListener but still not working:
mp.reset();
mp.setDataSource(musicUri);
mp.prepareAsync();
// mp.prepare();
mp.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(final MediaPlayer mp, final int what,
final int extra) {
Log.e(Constant.TAG_MYREC, "Error occurred while playing audio.");
mp.stop();
return false;
}
});
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});