How to check if file is supported by android videoview by code?

≯℡__Kan透↙ 提交于 2020-01-14 09:37:42

问题


I am trying to play video by Android videoview. Here is my code:

super.onCreate(savedInstanceState);
setContentView(R.layout.video);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();

This works fine, however some phones still show dialog box with title "Cannot play video".

My question is how to disable this notification window? I mean, can I check if the video file is supported or not before calling videoView.start()? Or can I disable or prevent calling the system popup notification window?

I would like to simply skip the video if not supported by the phone, without the notification window.


回答1:


I used setOnErrorListener before you start the VideoView to check if video will play.

    // Restart if PROBLEM
    myVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        public boolean onError(MediaPlayer mp, int what, int extra) {
            // TODO Auto-generated method stub
            Intent intent = getIntent();
            overridePendingTransition(0, 0);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

            finish();

            overridePendingTransition(0, 0);
            startActivity(intent);

            return true;
        }

    });
    myVideoView.start();



回答2:


I found another easy way of using MediaPlayer to solve part of this problem.

try {
    MediaPlayer mp = MediaPlayer.create(this, uri);
    mp.release();
} catch (Exception e) {
    Log.e("MediaPlayer", "can NOT play: " + uri);
}

The above code can filter out most videos that not supported by VideoView, but it's not perfect, because I found some not-supported mkv/mpg videos also pass the above test.

Anyway, it's another line of thought. I posted it here, and hope someone can improve it.



来源:https://stackoverflow.com/questions/10732804/how-to-check-if-file-is-supported-by-android-videoview-by-code

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