Tamas
Tamas

Reputation: 353

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

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.

Upvotes: 5

Views: 4148

Answers (2)

Jason Lu
Jason Lu

Reputation: 31

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.

Upvotes: 0

Hannes
Hannes

Reputation: 31

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

Upvotes: 3

Related Questions