Get supported Codec for Android device

Viewed 11917

Is there a way to ask an Android device what audio and video Codecs it supports for encoding?

I found devices that do not support some of the codecs listed as mandatory in http://developer.android.com/guide/appendix/media-formats.html and there seem to be devices supporting additional codec not listed there.

4 Answers

The simplest way is using

MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos

It returns a array of all encoders and decoders available on your devices like this image.

And then, you can use filter to query the specific encoders and decoders you are looking for. For example:

MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos.filter {
    it.isEncoder && it.supportedTypes[0].startsWith("video")
}

This returns all available video encoders.

Here is an updated code based on Jonson's answer, written in Kotlin and not using deprecated methods:

 fun getCodecForMimeType(mimeType: String): MediaCodecInfo? {
    val mediaCodecList = MediaCodecList(MediaCodecList.REGULAR_CODECS)
    val codecInfos = mediaCodecList.codecInfos
    for (i in codecInfos.indices) {
        val codecInfo = codecInfos[i]
        
        if (!codecInfo.isEncoder) {
            continue
        }
        
        val types = codecInfo.supportedTypes
        for (j in types.indices) {
            if (types[j].equals(mimeType, ignoreCase = true)) {
                return codecInfo
            }
        }
    }
    
    return null
}
Related