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.
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);
@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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With