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

自作多情 提交于 2019-12-07 00:36:31

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

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

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!

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