我实现了我的音乐播放器功能,有时ogg文件会抛出(1,-1010)那意味着
MEDIA_ERROR_UNKNOWN / MEDIA_ERROR_MALFORMED
错误。文件格式没有任何问题,因为有时文件加载正确(假设70%正确30%得到该错误)。
private void loadAudio(){
if(!readyToPlay){
elapsedTimeStart.setText("");
elapsedTimeEnd.setText(context.getString(R.string.loading));
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaplayer.setDataSource(audioURL);
mediaplayer.prepareAsync();
mediaplayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
double ratio = percent / 1000.0;
bufferingLevel = (int)(mp.getDuration() * ratio);
seekBar.setSecondaryProgress(bufferingLevel/SECOND);
}
});
mediaplayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.i("lang","on error " +what +" extra "+ extra);
switch (what) {
case MEDIA_ERROR_UNKNOWN:
Log.i(TAG, "MEDIA_ERROR_UNKNOWN");
break;
case MEDIA_ERROR_SERVER_DIED:
Log.i(TAG, "MEDIA_ERROR_SERVER_DIED");
Toast.makeText(context, context.getString(R.string.problems_connecting_try_again), Toast.LENGTH_SHORT).show();
break;
}
switch (extra) {
case MEDIA_ERROR_IO:
Log.i(TAG, "MEDIA_ERROR_IO");
break;
case MEDIA_ERROR_MALFORMED:
Log.i(TAG, "MEDIA_ERROR_MALFORMED");
break;
case MEDIA_ERROR_UNSUPPORTED:
Log.i(TAG, "MEDIA_ERROR_UNSUPPORTED");
break;
case MEDIA_ERROR_TIMED_OUT:
Log.i(TAG, "MEDIA_ERROR_TIMED_OUT");
Toast.makeText(context, context.getString(R.string.problems_connecting_try_again), Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.i("lang", " SUCCEFULLY PREPARED");
readyToPlay = true;
durationTime = mp.getDuration();
durationHour = durationTime /HOUR;
durationMint = (durationTime %HOUR)/MINUTE;
durationSec = (durationTime %MINUTE)/SECOND;
elapsedTimeStart.setText(String.format("%02d:%02d:%02d",currentHour,currentMin,currentSec));
elapsedTimeEnd.setText(String.format("%02d:%02d:%02d", durationHour, durationMint, durationSec));
seekBar.setMax(durationTime /SECOND);
play();
}
});
} catch (IllegalArgumentException|SecurityException|IllegalStateException|IOException e) {
Log.i("lang","Exception!");
e.printStackTrace();
}
catch (Exception e){
Log.i("lang", "Exception!! smthing goes wrong ");
e.printStackTrace();
}
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaplayer != null && fromUser) {
timeElapsed = durationTime * progress / 100;
mediaplayer.seekTo(timeElapsed);
if (durationTime / SECOND == timeElapsed / SECOND)
stop();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}}
编辑:我调试我的程序以及在mediaplayer.prepareAsync()
onPrepared
方法之后触发的第一个回调。有没有办法检查此状态(MEDIA_ERROR_UNKNOWN / MEDIA_ERROR_MALFORMED)何时发生,并调用loadAudio()
函数以获得正确的跟踪?
答案 0 :(得分:1)
网址存在问题,网址不正确或服务器未通过此网址发送正确的回复。
这就是为什么这个URL给出了这个错误MEDIA_ERROR_MALFORMED
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
LogUtils.e(TAG, "onError() called : what=="+what);
LogUtils.e(TAG, "onError() called : extra=="+extra);
return false;
}
});