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

旧巷老猫 提交于 2019-12-08 06:57:04

问题


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

回答1:


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



回答2:


IOException is thrown when setDataSource(String path) is called and file does not exist.

        Uri uri = Uri.parse(url)
        final String scheme = uri.getScheme();
        if ("file".equals(scheme)) {
            path = uri.getPath();
            mMediaPlayer.setDataSource(path); // IOException
        } else {
            mMediaPlayer.setDataSource(getContext(), uri);
        }



回答3:


Android Mediaplayer doesn't throw an exception if the link is wrong with prepareAsync().
For prepareAsync try to catch your errors using:

onError(MediaPlayer mp, int what, int extra);

using MediaPlayer.setOnErrorListener().

Instead use mMediaPlayer.prepare();

Another alternative is to use

URLUtil.isValidUrl(url)

to check right away if your link exists!



来源:https://stackoverflow.com/questions/43065662/android-mediaplayer-doesnt-throw-io-exception-if-file-not-available-at-the-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!