Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android check for valid video file

Tags:

android

How to check a video file is valid or not without checking its extension(.mp3 or .3gp etc). Means how to check the video file on SD card is supported by device or not?

Is there any api to validate video file in android 4.0 and above?

My Scenario: I am playing video on VideoView after downloading it and play it from local SD card after download success. Next time when a request for same video, then checks in SD card, if found then start playing it(No download in this case). But sometimes network error or app kill interrupt the downloading(in this case the video file is not completely downloaded) so the downloaded file is corrupted and VideoView is unable to play this file. So how to detect this corrupted file.

like image 646
user1041858 Avatar asked Oct 28 '25 04:10

user1041858


2 Answers

Here is the code that worked for me:

 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
 retriever.setDataSource(context, Uri.fromFile(fileToTest));

 String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
 boolean isVideo = "yes".equals(hasVideo);
like image 162
Alex Avatar answered Oct 30 '25 18:10

Alex


@Alex had given right answer but still some problems are there as like @Kirill mention in comment that setDataSource often throws java.lang.RuntimeException: setDataSource failed exception. So Here is the function check for valid video file

private boolean videoFileIsCorrupted(String path){

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {
        retriever.setDataSource(myContext, Uri.parse(path));
    }catch (Exception e){
        e.printStackTrace();
        return false;
     }

String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
return "yes".equals(hasVideo);
}
like image 24
Manthan Patel Avatar answered Oct 30 '25 19:10

Manthan Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!