简体   繁体   English

如何在 VideoView 上捕获“抱歉,此视频无法播放”错误

[英]How to catch “Sorry, This video cannot be played” error on VideoView

I have a VideoView and I am streaming videos from a remote server.我有一个 VideoView,我正在从远程服务器流式传输视频。 Most of the times It would play the videos very smoothly.大多数时候它会非常流畅地播放视频。 But sometimes, it displays an error message "Sorry, This video cannot be played".但有时,它会显示一条错误消息“抱歉,此视频无法播放”。 I have a hunch that this is more on the supported video formats.我有预感,这更多的是支持的视频格式。 However, I don't know which are the supported formats.但是,我不知道哪些是支持的格式。 My question is "How can I catch this error (eg Prevent the error message from appearing)"?我的问题是“我怎样才能捕捉到这个错误(例如防止出现错误消息)”? I am using Android 2.2 on this project.我在这个项目上使用 Android 2.2。 Any advice would be greatly appreciated.任何建议将不胜感激。 :) :)

Try using setOnErrorListener : the documentation says If no listener is specified, or if the listener returned false, VideoView will inform the user of any errors.尝试使用setOnErrorListener :文档说如果没有指定侦听器,或者如果侦听器返回 false,VideoView 将通知用户任何错误。 , so I'm assuming if you set one and return true it will not show the user error. ,所以我假设如果你设置一个并返回 true 它不会显示用户错误。

The code I used for this:我用于此的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    vView = (VideoView) findViewById(R.id.videoView1);

    vSource = "android.resource://com.domain.android/"
            + R.raw.introductionportrait;
    vView.setVideoURI(Uri.parse(vSource));

    vView.setOnErrorListener(mOnErrorListener);
    vView.requestFocus();
    vView.start();
}

private OnErrorListener mOnErrorListener = new OnErrorListener() {

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // Your code goes here
        return true;
    }
};

I prefer setting listeners like this within onCreate method.我更喜欢在 onCreate 方法中设置这样的监听器。 Hopefully helps someone out希望能帮助别人

videoView.setOnErrorListener(new OnErrorListener () {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Log.e(TAG, "Error playing video");
        return true;
    }
});

you can add code like below, it will close video view screen if any error occurred.您可以添加如下代码,如果发生任何错误,它将关闭视频查看屏幕。 Also, it will not display default popup of saying video can't play:)此外,它不会显示说视频无法播放的默认弹出窗口:)

 videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                finish();
                return true;
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM