How to tell if .3gp file is audio or video in android

◇◆丶佛笑我妖孽 提交于 2019-12-03 08:43:46

METADATA_KEY_HAS_VIDEO

If this key exists the media contains video content.

I figured out a way to do this:

public static boolean is3gpFileVideo(File mediaFile) { 
        int height = 0;
        try {
            MediaPlayer mp = new MediaPlayer();
            FileInputStream fs;
            FileDescriptor fd;
            fs = new FileInputStream(mediaFile);
            fd = fs.getFD();
            mp.setDataSource(fd);
            mp.prepare(); 
            height = mp.getVideoHeight();
            mp.release();
        } catch (Exception e) {
            Log.e(TAG, "Exception trying to determine if 3gp file is video.", e);
        }
        return height > 0;
    }

So to figure out if a media file has video you can use this.. Probably not the most efficient way to do it but for something done rarely in your application it seems like a reasonable solution.

I have not tested this solution, but you could try to get a thumbnail for the video.

Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(videoPath,
            MediaStore.Images.Thumbnails.MINI_KIND);

If the thumbnail is null then it's audio, otherwise it's video.

I have tested Alessandro Roaro's solution and it works for me. I use it currently in my app.

I quote:

I have not tested this solution, but you could try to get a thumbnail for the video. If the thumbnail is null then it's audio, otherwise it's video.

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