Search code examples
androidmime-typesandroid-mediaplayer3gp

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


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?


Solution

  • 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.