Search code examples
androidandroid-mediaplayer

Android Mediaplayer doesnt throw IO exception If file not available at the link


I am starting mediaplayer with a URL. The URL suppose to link to a video/Audio . But i have deleted the video/audio file from the location, hence i would like to expect a IOException id there is nothing available at that link.

But i am not getting the IO exception. Instead mediaplayer itself try to go to the linkl 10 times and finally throw the error on onErrorListner. follwings are the logs printed while mediaPlayer is preparing.

Note: - my url is not for local storaged file!! its for server side file.

 E/NuCachedSource2: source returned error -1, 10 retries left
 E/NuCachedSource2: source returned error -1, 9 retries left
 E/NuCachedSource2: source returned error -1, 8 retries left
 E/NuCachedSource2: source returned error -1, 7 retries left
 E/NuCachedSource2: source returned error -1, 6 retries left
 E/NuCachedSource2: source returned error -1, 5 retries left
 E/NuCachedSource2: source returned error -1, 4 retries left
 E/NuCachedSource2: source returned error -1, 3 retries left
 E/NuCachedSource2: source returned error -1, 2 retries left
 E/NuCachedSource2: source returned error -1, 1 retries left
 E/NuCachedSource2: source returned error -1, 0 retries left
 E/GenericSource: Failed to init from data source!

I do not want to wait so long that mediaplayer try to reconnect with the same URL 10 time. I want the IOException or error immediately on the very first time.

following is my code. Kindly help!!

  mMediaPlayer = new MediaPlayer();
  mMediaPlayer.setDataSource(getContext(), Uri.parse(url));
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setLooping(false);
            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                    OnErrorReceive("Something is wrong with media player states");

                    return false;
                }
            });
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
    enter code here
               strong text     mMediaPlayer.start();

                }
            });
            mMediaPlayer.prepareAsync();

Solution

  • If you are playing with remote media resources, I'd recommend other mediaPlayer implementation like google/ExoPlayer2.

    The retry count of Android default player is 10. There is no error handling on HTTP 404 response.

    struct NuCachedSource2 : public DataSource {
    
    ...
    enum {
        kMaxNumRetries = 10,
    };
    

    google/ExoPlayer2 has 2. But you can change if you want. my commit in github

          if (retryAction == DONT_RETRY_FATAL) {
            fatalError = currentError;
          } else if (retryAction != DONT_RETRY) { // DONT_RETRY = 2
            errorCount = retryAction == RETRY_RESET_ERROR_COUNT ? 1 : errorCount + 1;
            start(getRetryDelayMillis());
          }