Expiredmind
Expiredmind

Reputation: 808

MediaPlayer error(1,-1010) MEDIA_ERROR_UNKNOWN/ MEDIA_ERROR_MALFORMED

Im implemented my music player function and sometimes the ogg files throws the (1,-1010) thats means

MEDIA_ERROR_UNKNOWN/ MEDIA_ERROR_MALFORMED

error. There is nothing wrong with file format because sometimes the file loading correcly (lets say 70% correcly 30% got that error).

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) {
            }
        });
    }}

EDIT: I debug my program and the first callback which trigger after mediaplayer.prepareAsync() is onPrepared method. Is there a way to check when the (MEDIA_ERROR_UNKNOWN/ MEDIA_ERROR_MALFORMED) occurs on this state and recall loadAudio() function to get correct track?

Upvotes: 0

Views: 2577

Answers (1)

Vishal Chhodwani
Vishal Chhodwani

Reputation: 2577

There is problem with URL, the URL is incorrect or the server not sending proper response by this URL. Thats why this URL give this Error MEDIA_ERROR_MALFORMED

EDIT

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;
            }
        });

Upvotes: 1

Related Questions