本文介绍了Android Mediaplayer不会引发IO异常如果文件在链接上不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用URL启动mediaplayer.该URL应该链接到视频/音频.但是我已经从该位置删除了视频/音频文件,因此我希望得到一个IOException id,该链接没有可用的内容.

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.

但是我没有得到IO异常.相反,mediaplayer本身会尝试访问链接10次,最后将错误抛出onErrorListner上.以下是mediaPlayer准备过程中打印的日志.

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.

注意:-我的网址不适用于本地存储文件!!它用于服务器端文件.

 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!

我不想等待太久,以使mediaplayer尝试使用相同的URL重新连接10次.我想在第一次就立即出现IOException或错误.

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

推荐答案

如果您正在使用远程媒体资源,我建议您使用其他mediaPlayer实现,例如 google/ExoPlayer2 .

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

Android默认播放器的重试计数为10.HTTP404响应没有错误处理.

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具有2.但是您可以根据需要进行更改. 我在github中提交

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

这篇关于Android Mediaplayer不会引发IO异常如果文件在链接上不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 06:54