3

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();
            }
        });
Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

2 Answers2

4
mMediaPlayer.setOnErrorListener(new OnErrorListener() {
    @Override
    public boolean onError(final MediaPlayer mp, final int what,
            final int extra) {
        Log.e("Error occurred while playing audio.");
        mp.stop();

You might be calling getDuration before the file is fully loaded. See if the solution to this question works for you.

Community
  • 1
  • 1
Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
0

There are multiple cause of MediaPlayer error, you can check values of what and extra in MediaPlayer.OnErrorListener.onError, and do action related to the error cause such as show Toast to notify user.

When file in the Uri is not available, it will return extra with value -1004, which is according to the docs is File or network related operation errors.

Here is another link to Error Codes in MediaType documentation

The code would be like:

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.i(TAG, "onError, what: " + what + ", extra: " + extra);

    if (extra == MediaPlayer.MEDIA_ERROR_IO) {
        // TODO Show Toast
    }


    mp.stop();

    // If you choose not to call onCompletion
    return true;
}
saggaf.arsyad
  • 801
  • 1
  • 7
  • 11