在Android中使用MediaPlayer时,如何显示无法查找视频错误

时间:2016-03-21 06:12:45

标签: android android-mediaplayer

使用网络视频文件时,如何显示找不到错误?OnErrorListener无法正常工作。 以下是onSurfaceTextureAvailable中的代码,当网络文件无法获取时,两者都没有反应。

try {

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(FILE_URL));
    mMediaPlayer.setSurface(surface);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.prepareAsync();

    // Play video when the media source is ready for playback.
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.start();
            Context context = getApplicationContext();
            CharSequence text = "ok";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    });
    mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener(){
        @Override
        public boolean onError(MediaPlayer mediaPlayer,int what,int extra){
            Context context = getApplicationContext();
            CharSequence text = "error";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            return true;
        }
    });

} catch (IllegalArgumentException e) {
    Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
    Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
    Log.d(TAG, e.getMessage());
} catch (java.io.IOException e) {
    Log.d(TAG, e.getMessage());
}

1 个答案:

答案 0 :(得分:-1)

你应该在将文件解析为uri之前检查文件。当文件不存在时,你可以给消息干杯。但如果视频源是网络,你可以在MediaPlayer.OnErrorListener中收到错误。