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

2019-03-22 02:48发布

问题:

I have an app which allows user to import media (videos, photos, audio) which will then be managed by the application (as evidence). I've found that some audio recording apps will save audio in .3gp format (specifically Whats App messenger). If i get the mime type using the following code:

MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

it will come back as video/3gp which my app takes to mean it is of the type "video" and tries to make thumbnails and what not (I use the first part of the mime type to determine what type my app categorizes it as). However it is audio so certain things I expect to work will not work (such as creating a thumbnail for the video). Are there any libraries or anything available by android which would allow me to tell if the file is a video audio only? I suppose I can try to make a thumbnail and if that fails assume the file is audio, but that's a bit of a stretch given a number of other problems could go wrong in making a thumbnail. Any ideas?

回答1:

METADATA_KEY_HAS_VIDEO

If this key exists the media contains video content.



回答2:

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.



回答3:

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.



回答4:

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