Search code examples
androidfilekotlinvideomime-types

Check 3gp file is audio/video in Android?


Im trying to get mimetype of video files using below code.

Porblem: I have a file saved as audio but its mimetype is video/3gpp and it is also playing as Audio. . How to detect this kind of file which is audio but mimetype is video/3gpp in its details so i can exclude it from list.

fun getFileMimeType(url: String): String {

        var type: String = ""

        try {
            if (url.lastIndexOf(".") != -1) {
                val ext: String = url.substring(url.lastIndexOf(".") + 1)
                val mime = MimeTypeMap.getSingleton()
                type = mime.getMimeTypeFromExtension(ext)!!
            }

        } catch (e: Exception) {
        }

        return type

    }

Solution

  • Assuming this is a local file, you can use MediaMetadataRetriever to get Metadata information about the media file.

    Specifically, you can check if the METADATA_KEY_HAS_VIDEO is set to true.

    An example in Kotlin:

            val metadataRetriever = MediaMetadataRetriever()
            metadataRetriever.setDataSource(context, Uri.fromFile(your3GPPFile))
    
            val hasVideoKey = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO)
            if (hasVideoKey == "yes") {
              //Include in your list or do whatever work 
              //you need here
            }
            
    

    You can also use ExoPlayer and load the file, and then get a list of the tracks using

    player.getCurrentTrackGroups()

    This will return an array of tracks which you can loop through and parse the format to check if they include a video track.